Dependencies with Agents

Inject variables into agent context with dependencies.

Dependencies are a way to inject variables into your Agent context. The dependencies parameter accepts a dictionary containing functions or static variables that are automatically resolved before the agent runs.

Note

You can use dependencies to inject memories, dynamic few-shot examples, "retrieved" documents, etc.

Basic usage

You can reference the dependencies in your agent instructions or user message.

1from kern.agent import Agent
2from kern.models.openai import OpenAIResponses
3
4agent = Agent(
5 model=OpenAIResponses(id="gpt-5.2"),
6 dependencies={"name": "John Doe"},
7 instructions="You are a story writer. The current user is {name}."
8)
9
10agent.print_response("Write a 5 second short story about {name}")
Note

Dependencies can be static values or callable functions. When using functions, they are automatically executed at runtime before the agent runs, and their return values are used as the dependency values.

Tip

You can set dependencies and add_dependencies_to_context on Agent initialization, or pass them dynamically to the run(), arun(), print_response() and aprint_response() methods.

Adding dependencies to context

Set add_dependencies_to_context=True to add the entire list of dependencies to the user message. This way you don't have to manually add the dependencies to the instructions.

1import json
2
3from kern.agent import Agent
4from kern.models.openai import OpenAIResponses
5
6
7def get_user_profile() -> str:
8 """Fetch and return the user profile.
9
10 Returns:
11 JSON string containing user profile information
12 """
13 # Get the user profile from the database (this is a placeholder)
14 user_profile = {
15 "name": "John Doe",
16 "experience_level": "senior",
17 }
18
19 return json.dumps(user_profile, indent=4)
20
21agent = Agent(
22 model=OpenAIResponses(id="gpt-5.2"),
23 dependencies={"user_profile": get_user_profile},
24 # We can add the entire dependencies dictionary to the user message
25 add_dependencies_to_context=True,
26 markdown=True,
27)
28
29agent.print_response(
30 "Get the user profile and tell me about their experience level.",
31 stream=True,
32)
33# Optionally pass the dependencies to the print_response method
34# agent.print_response(
35# "Get the user profile and tell me about their experience level.",
36# dependencies={"user_profile": get_user_profile},
37# stream=True,
38# )
Note

This adds the entire dependencies dictionary to the user message between <additional context> tags. The new user message looks like this:

1Get the user profile and tell me about their experience level.
2
3<additional context>
4{
5"user_profile": "{\n \"name\": \"John Doe\",\n \"experience_level\": \"senior\"\n}"
6}
7</additional context>

Access dependencies in tool calls and hooks

You can access the dependencies in tool calls and hooks by using the RunContext object.

1from kern.agent import Agent
2from kern.db.sqlite import SqliteDb
3from kern.models.openai import OpenAIResponses
4from kern.run import RunContext
5
6def get_user_profile(run_context: RunContext) -> str:
7 """Get the user profile."""
8 return run_context.dependencies["user_profiles"][run_context.user_id]
9
10agent = Agent(
11 model=OpenAIResponses(id="gpt-5.2"),
12 db=SqliteDb(db_file="tmp/agents.db"),
13 tools=[get_user_profile],
14 dependencies={
15 "user_profiles": {
16 "user_1001": {"name": "John Doe", "experience_level": "senior"},
17 "user_1002": {"name": "Jane Doe", "experience_level": "junior"},
18 }
19 },
20)
21
22agent.print_response("Get the user profile for the current user and tell me about their experience level.", user_id="user_1001", stream=True)

See the RunContext schema for more information.

Learn more