# Context Engineering (/context/overview)



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.

```bash
uv pip install -U agno openai sqlalchemy
```

Set `OPENAI_API_KEY` to your OpenAI API key in the shell where you run the example:

```bash
export OPENAI_API_KEY="your-api-key"
```

```python support_agent.py
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 [#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](/knowledge/overview)            | Retrieved content from documents and data      | Domain grounding and source-backed answers  |
| [Memory](/memory/overview)                  | Persistent facts associated with a user        | Preferences and details that cross sessions |
| [Chat history](/history/overview)           | Messages from earlier runs in one session      | Multi-turn continuity                       |
| [Session state](/state/overview)            | Application data stored with the session       | Carts, task progress, plans, and counters   |
| [Dependencies](/dependencies/overview)      | 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 [#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](/sessions/session-summaries)                                |
| Reduce stored tool-result context      | [Context compression](/compression/overview)                                    |
| Retrieve relevant domain content       | [Knowledge search](/knowledge/concepts/search-and-retrieval/overview)           |
| 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](/agents/debugging-agents) when behavior suggests the model received missing, stale, or conflicting context.

## Context Caching [#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](https://platform.openai.com/docs/guides/prompt-caching)
* [Anthropic prompt caching](https://docs.claude.com/en/docs/build-with-claude/prompt-caching)
* [Anthropic caching with Agno](/models/providers/native/anthropic/usage/prompt-caching)
* [OpenRouter prompt caching](https://openrouter.ai/docs/features/prompt-caching)

## Next Steps [#next-steps]

<CardGroup cols="3">
  <Card title="Agent Context" icon="robot" iconType="duotone" href="/context/agent/overview">
    Configure system messages and instructions for agents.
  </Card>

  <Card title="Team Context" icon="users" iconType="duotone" href="/context/team/overview">
    Configure context for a leader and its members.
  </Card>

  <Card title="Chat History" icon="history" iconType="duotone" href="/history/overview">
    Select previous messages for model context.
  </Card>
</CardGroup>
