Context Engineering
Control the instructions, data, history, and tools sent to a model for each run.
Context engineering controls what a model sees when an agent or team runs. Product teams use it to give the model the right instructions and application data while keeping each request focused.
uv pip install -U agno openai sqlalchemySet OPENAI_API_KEY to your OpenAI API key in the shell where you run the example:
export OPENAI_API_KEY="your-api-key"from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
agent = Agent(
model=OpenAIResponses(id="gpt-5.4-mini"),
db=SqliteDb(db_file="tmp/support.db"),
instructions=[
"Answer product questions clearly and concisely.",
"The customer's plan is {plan}.",
],
session_state={"plan": "enterprise"},
add_history_to_context=True,
num_history_runs=3,
)
agent.print_response(
"Which support channels can I use?",
user_id="customer-42",
session_id="support-thread-7",
)The system message carries the agent's instructions and resolved plan. The user message carries the current request. Up to three previous runs from the same stored session can also enter the model context.
Sources of Context
| Source | What it contributes | Use it for |
|---|---|---|
| Description and instructions | Stable role, behavior, and constraints | Product behavior that applies across runs |
| Run input | The current user request | The task to complete now |
| Knowledge | Retrieved content from documents and data | Domain grounding and source-backed answers |
| Memory | Persistent facts associated with a user | Preferences and details that cross sessions |
| Chat history | Messages from earlier runs in one session | Multi-turn continuity |
| Session state | Application data stored with the session | Carts, task progress, plans, and counters |
| Dependencies | Static or callable values resolved at run time | Request-specific application data |
| Tool definitions and results | Available operations and their outputs | Reading data and taking actions |
additional_context and additional_input | Explicit system or message context | Few-shot examples and custom context blocks |
Agno can assemble these sources for each run. Enable only the sources the model needs for the current use case.
Control Context Size
| Requirement | Configuration |
|---|---|
| Include recent conversation turns | add_history_to_context=True with num_history_runs or num_history_messages |
| Condense a long conversation | Session summaries |
| Reduce stored tool-result context | Context compression |
| Retrieve relevant domain content | Knowledge search |
| Add runtime values to the user message | add_dependencies_to_context=True |
| Add session state as a context block | add_session_state_to_context=True |
Start with the smallest set that supports the task. Inspect model messages in debug mode when behavior suggests the model received missing, stale, or conflicting context.
Context Caching
Some model providers cache repeated prompt prefixes. Provider requirements and pricing differ. Keep stable instructions consistent between requests, place changing data in the appropriate runtime fields, and verify the selected provider's caching behavior.
- OpenAI prompt caching
- Anthropic prompt caching
- Anthropic caching with Agno
- OpenRouter prompt caching