This lesson clarifies what context_schema does: it is not long-term memory, but runtime context passed into each call. It is especially useful for user preferences, tenant information and permissions.
context is structured information passed in for a single Agent call. It is for data the current run needs to know but does not necessarily need to store in long-term state, such as user preferences, the current organization or a permission scope.
| Concept | Lifetime | Typical uses |
|---|---|---|
| Runtime context | During one invoke/ainvoke call | User preferences, tenant id, request source, permissions |
| Agent state | Can be saved across calls through a checkpointer | Conversation memory, collected fields, workflow progress |
| System prompt | Agent behavior instructions | Role, rules, response style |
context is only available at runtime. To make the model use it, you usually need prompt instructions or an explicit tool/middleware read.
The notebook uses a dataclass to define a color-preference context. Passing context_schema to create_agent tells the Agent what the runtime context looks like.
from dataclasses import dataclass
@dataclass
class ColourContext:
favourite_colour: str = "blue"
least_favourite_colour: str = "yellow"
agent = create_agent(
model=deepseek_model(),
context_schema=ColourContext,
)context_schema provides type constraints and the runtime access shape. It does not automatically write these fields into model-visible messages.
If you want the model to answer "What is my favourite colour?" directly, the model needs to know that it should use runtime context. The notebook uses system_prompt to explicitly tell the model to read user preferences.
response = agent.invoke(
{"messages": [HumanMessage(content="What is my favourite colour?")]},
context=ColourContext(),
system_prompt="Use the runtime context to answer questions about the user's preferences.",
)
print(response["messages"][-1].content)Declaring context_schema does not mean the model naturally knows the field values. A more reliable approach is to let tools read context through ToolRuntime.
A tool function can declare a runtime: ToolRuntime parameter. At runtime, LangChain places the current call's context on runtime.context.
from langchain.tools import tool, ToolRuntime
@tool
def get_favourite_colour(runtime: ToolRuntime) -> str:
"""Get the favourite colour of the user"""
return runtime.context.favourite_colour
@tool
def get_least_favourite_colour(runtime: ToolRuntime) -> str:
"""Get the least favourite colour of the user"""
return runtime.context.least_favourite_colourThe tool description tells the model that a tool can read the favourite colour. After the model calls the tool, it receives an explicit return value, which is more stable than asking the model to infer context fields.
When creating the Agent, pass both tools and context_schema. When calling it, pass context=ColourContext(...), and the tools can read the current call's values.
agent = create_agent(
model=deepseek_model(),
tools=[get_favourite_colour, get_least_favourite_colour],
context_schema=ColourContext,
)
response = agent.invoke(
{"messages": [HumanMessage(content="What is my favourite colour?")]},
context=ColourContext(favourite_colour="green"),
)
print(response["messages"][-1].content)The user asks about a preference.
The model calls the tool that reads the preference.
runtime.context returns the value passed into this call.
The model answers based on the tool result.
If information needs to be updated and saved by the Agent across multiple turns, use state. If information is passed in by an external system on each call, use context.