Skip to content

Chapter 07 · Context compression and prompt caching ​

Long tasks inflate context. Deep Agents built-in offload/summarization compresses history and large tool results; on Anthropic/Bedrock, static prompt sections can also enable prompt caching to cut per-turn cost.

Mechanics and design choices ​

Context engineering = input context + runtime context + compression + long-term memory. Compression is not “delete history” but keeping key conclusions so long tasks continue. Prompt caching needs model-side support; not all providers offer it, or offer it free.

Example ​

python
import os
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model=os.environ["MODEL_ID"],
    api_key=os.environ["DEEPAGENT_API_KEY"],
    base_url=os.environ.get("DEEPAGENT_BASE_URL", "https://api.openai.com/v1"),
)

from deepagents import create_deep_agent

agent = create_deep_agent(
    model=model,
    system_prompt="Stay under 100 words. Prefer summaries over transcripts.",
)
result = agent.invoke({
    "messages": [
        {"role": "user", "content": "Here is a long log. Summarize it in one line: " + "deploy step ok; " * 300},
    ]
})
print(result["messages"][-1].content)

Run and verify ​

API key required. The answer is a short summary (under 100 words), not a transcript of the log.

Pitfalls ​

Compression is not free: if critical information is lost, downstream decisions drift. Give the task a retention checklist (user intent, completed steps, conclusions to verify) and check against it after compression.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What does compression target?

2. Is prompt caching always free?

3. What to do after compression?

4. Where should static prompt parts live?

Hands-on exercises ​

  1. Build a conversation with 5 completed steps and verify the key steps survive.
  2. Add an explicit retention checklist to the system prompt.
  3. Compare latency and cost with and without prompt caching.

Official references ​

Verified on 2026-09-17 with Deep Agents 0.7.15.