docs/context.md
Context is an overloaded term. There are two main classes of context you might care about:
on_handoff, in lifecycle hooks, etc.This is represented via the [RunContextWrapper][agents.run_context.RunContextWrapper] class and the [context][agents.run_context.RunContextWrapper.context] property within it. The way this works is:
Runner.run(..., context=whatever)).RunContextWrapper[T], where T represents your context object type which you can access via wrapper.context.For some runtime-specific callbacks, the SDK may pass a more specialized subclass of RunContextWrapper[T]. For example, function-tool lifecycle hooks typically receive ToolContext, which also exposes tool-call metadata like tool_call_id, tool_name, and tool_arguments.
The most important thing to be aware of: every agent, tool function, lifecycle etc for a given agent run must use the same type of context.
You can use the context for things like:
!!! danger "Note"
The context object is **not** sent to the LLM. It is purely a local object that you can read from, write to and call methods on it.
Within a single run, derived wrappers share the same underlying app context, approval state, and usage tracking. Nested [Agent.as_tool()][agents.agent.Agent.as_tool] runs may attach a different tool_input, but they do not get an isolated copy of your app state by default.
RunContextWrapper exposes[RunContextWrapper][agents.run_context.RunContextWrapper] is a wrapper around your app-defined context object. In practice you will most often use:
wrapper.context][agents.run_context.RunContextWrapper.context] for your own mutable app state and dependencies.wrapper.usage][agents.run_context.RunContextWrapper.usage] for aggregated request and token usage across the current run.wrapper.tool_input][agents.run_context.RunContextWrapper.tool_input] for structured input when the current run is executing inside [Agent.as_tool()][agents.agent.Agent.as_tool].wrapper.approve_tool(...)][agents.run_context.RunContextWrapper.approve_tool] / [wrapper.reject_tool(...)][agents.run_context.RunContextWrapper.reject_tool] when you need to update approval state programmatically.Only wrapper.context is your app-defined object. The other fields are runtime metadata managed by the SDK.
If you later serialize a [RunState][agents.run_state.RunState] for human-in-the-loop or durable job workflows, that runtime metadata is saved with the state. Avoid putting secrets in [RunContextWrapper.context][agents.run_context.RunContextWrapper.context] if you intend to persist or transmit serialized state.
Conversation state is a separate concern. Use result.to_input_list(), session, conversation_id, or previous_response_id depending on how you want to carry turns forward. See results, running agents, and sessions for that decision.
import asyncio
from dataclasses import dataclass
from agents import Agent, RunContextWrapper, Runner, function_tool
@dataclass
class UserInfo: # (1)!
name: str
uid: int
@function_tool
async def fetch_user_age(wrapper: RunContextWrapper[UserInfo]) -> str: # (2)!
"""Fetch the age of the user. Call this function to get user's age information."""
return f"The user {wrapper.context.name} is 47 years old"
async def main():
user_info = UserInfo(name="John", uid=123)
agent = Agent[UserInfo]( # (3)!
name="Assistant",
tools=[fetch_user_age],
)
result = await Runner.run( # (4)!
starting_agent=agent,
input="What is the age of the user?",
context=user_info,
)
print(result.final_output) # (5)!
# The user John is 47 years old.
if __name__ == "__main__":
asyncio.run(main())
RunContextWrapper[UserInfo]. The tool implementation reads from the context.UserInfo, so that the typechecker can catch errors (for example, if we tried to pass a tool that took a different context type).run function.ToolContextIn some cases, you might want to access extra metadata about the tool being executed — such as its name, call ID, or raw argument string.
For this, you can use the [ToolContext][agents.tool_context.ToolContext] class, which extends RunContextWrapper.
from typing import Annotated
from pydantic import BaseModel, Field
from agents import Agent, Runner, function_tool
from agents.tool_context import ToolContext
class WeatherContext(BaseModel):
user_id: str
class Weather(BaseModel):
city: str = Field(description="The city name")
temperature_range: str = Field(description="The temperature range in Celsius")
conditions: str = Field(description="The weather conditions")
@function_tool
def get_weather(ctx: ToolContext[WeatherContext], city: Annotated[str, "The city to get the weather for"]) -> Weather:
print(f"[debug] Tool context: (name: {ctx.tool_name}, call_id: {ctx.tool_call_id}, args: {ctx.tool_arguments})")
return Weather(city=city, temperature_range="14-20C", conditions="Sunny with wind.")
agent = Agent(
name="Weather Agent",
instructions="You are a helpful agent that can tell the weather of a given city.",
tools=[get_weather],
)
ToolContext provides the same .context property as RunContextWrapper,
plus additional fields specific to the current tool call:
tool_name – the name of the tool being invokedtool_call_id – a unique identifier for this tool calltool_arguments – the raw argument string passed to the tooltool_namespace – the Responses namespace for the tool call, when the tool was loaded through tool_namespace() or another namespaced surfacequalified_tool_name – the tool name qualified with the namespace when one is availableUse ToolContext when you need tool-level metadata during execution.
For general context sharing between agents and tools, RunContextWrapper remains sufficient. Because ToolContext extends RunContextWrapper, it can also expose .tool_input when a nested Agent.as_tool() run supplied structured input.
When an LLM is called, the only data it can see is from the conversation history. This means that if you want to make some new data available to the LLM, you must do it in a way that makes it available in that history. There are a few ways to do this:
instructions. This is also known as a "system prompt" or "developer message". System prompts can be static strings, or they can be dynamic functions that receive the context and output a string. This is a common tactic for information that is always useful (for example, the user's name or the current date).input when calling the Runner.run functions. This is similar to the instructions tactic, but allows you to have messages that are lower in the chain of command.