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 sqlalchemy

Set 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

SourceWhat it contributesUse it for
Description and instructionsStable role, behavior, and constraintsProduct behavior that applies across runs
Run inputThe current user requestThe task to complete now
KnowledgeRetrieved content from documents and dataDomain grounding and source-backed answers
MemoryPersistent facts associated with a userPreferences and details that cross sessions
Chat historyMessages from earlier runs in one sessionMulti-turn continuity
Session stateApplication data stored with the sessionCarts, task progress, plans, and counters
DependenciesStatic or callable values resolved at run timeRequest-specific application data
Tool definitions and resultsAvailable operations and their outputsReading data and taking actions
additional_context and additional_inputExplicit system or message contextFew-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

RequirementConfiguration
Include recent conversation turnsadd_history_to_context=True with num_history_runs or num_history_messages
Condense a long conversationSession summaries
Reduce stored tool-result contextContext compression
Retrieve relevant domain contentKnowledge search
Add runtime values to the user messageadd_dependencies_to_context=True
Add session state as a context blockadd_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.

Next Steps