LangChain LangGraph
Module 1 Module 2 Module 3
Module 2 · Tools, State and Multi-Agent
1 2.1 MCP
2 2.1 Travel Agent
3 2.2 Runtime Context
4 2.2 State
5 2.3 Multi Agent
6 2.4 Wedding Planners
7 Bonus: RAG
8 Bonus: SQL
SUM Module Summary
HomeLangChainModule 2 · Tools, State and Multi-Agent2.2 Runtime Context
📓 Notebook 📖 Explained
LANGCHAIN MODULE 2 · LESSON 2.2a

Runtime Context: injecting information at runtime
context_schema · ToolRuntime · preference lookup

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.

1 What context is, and what it is not

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.

ConceptLifetimeTypical uses
Runtime contextDuring one invoke/ainvoke callUser preferences, tenant id, request source, permissions
Agent stateCan be saved across calls through a checkpointerConversation memory, collected fields, workflow progress
System promptAgent behavior instructionsRole, rules, response style
Focus of this lesson

context is only available at runtime. To make the model use it, you usually need prompt instructions or an explicit tool/middleware read.

2 Define context_schema

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.

Python
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,
)
What the schema does

context_schema provides type constraints and the runtime access shape. It does not automatically write these fields into model-visible messages.

3 Passing context alone is not enough

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.

Python
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)
Common misunderstanding

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.

4 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.

Python
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_colour
Why this is more reliable

The 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.

5 Give the context tools to the Agent

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.

Python
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)
1. User asks

The user asks about a preference.

2. Model chooses tool

The model calls the tool that reads the preference.

3. Tool reads context

runtime.context returns the value passed into this call.

4. Model answers

The model answers based on the tool result.

6 When to use Runtime Context

Boundary with State

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.