Technical Guide · 2026-05-31 · 7 min read
Optimizing Large Contexts with Prompt Caching
Stop paying for the same system prompts and codebase context on every single API call.
Long-context windows are useful until the latency and token costs make them impractical for iterative development. Prompt caching in Anthropic's Claude models allows developers to store frequently used prefixes—such as documentation, large codebase snapshots, or complex system instructions—reducing the time-to-first-token and the cost of repeat inputs.
The Problem with Stateless LLM Calls
Standard LLM interactions are stateless. If you send a 50k token codebase context to Claude to ask a simple question about a function, you pay for those 50k tokens. If you follow up with a second question, you pay for those same 50k tokens again. This creates a linear cost increase and significant latency overhead as the model re-processes the same prefix.
How Prompt Caching Works
Anthropic's prompt caching allows you to mark specific breakpoints in your prompt. The model caches the processed state of the tokens up to that breakpoint. When a subsequent request arrives with the exact same prefix, the model resumes from the cached state rather than re-computing the entire sequence.
- Cache Writes: The first time a prefix is cached, there is a slight surcharge.
- Cache Hits: Subsequent calls using the cached prefix are significantly cheaper and faster.
- TTL: Caches typically have a short time-to-live (TTL), meaning they expire if not reused within a specific window (usually 5 minutes).
Comparing Implementation Strategies
Depending on your workflow, you will likely use caching in one of three ways:
1. Static System Prompts
For agents with 10k+ tokens of instructions, caching the system prompt is a baseline requirement. This ensures that the agent's identity and constraints are loaded instantly without recurring costs.
2. Project-Specific Knowledge
When indexing a repository, you can cache the core library definitions or the AZMX.md project memory file. By keeping the project structure in the cache, the agent maintains state across a session without re-reading the entire directory tree.
3. Conversational History
In long chat threads, caching the previous turns prevents the exponential cost growth typically seen in multi-turn conversations. You move the cache breakpoint forward as the conversation evolves.
AZMX AI and Prompt Caching
Most AI coding tools operate as wrappers around a web-based IDE or a heavy Electron app. AZMX AI takes a different approach. Because it is a native Rust-based app (~7 MB), it minimizes local overhead, but the real efficiency comes from how it handles the LLM layer. Since AZMX AI uses a BYOK (Bring Your Own Key) model, users can leverage Anthropic's caching directly via their API keys.
While tools like Cursor or GitHub Copilot manage the context window behind a proprietary curtain, AZMX AI provides an approval-gated agent that interacts with your local PTY. When configured with Claude 3.5 Sonnet, the ability to cache the current project state means you can perform complex refactors across multiple files without the latency spikes associated with massive context uploads. You can read more about our architecture in the documentation.
Comparison with Other Tooling
Different agents handle context differently:
- Claude Code and Aider: These tools often rely on aggressive file-pruning or RAG (Retrieval-Augmented Generation) to keep prompts small. Prompt caching allows them to keep more context active without the cost penalty.
- Cline and Continue: These extensions often struggle with context window management in VS Code. A standalone native app like AZMX AI can manage the
AZMX.mdmemory file as a persistent cache prefix more efficiently. - Windsurf and Sourcegraph Cody: These utilize sophisticated indexing. Caching complements indexing by ensuring that once a piece of code is retrieved, it stays warm for the duration of the task.
Technical Implementation Details
To implement prompt caching, you must specify the cache_control block in your API request. Here is a conceptual example of how a request is structured for a long-context agent:
{
"model": "claude-3-5-sonnet-20240620",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Here is the entire codebase...",
"cache_control": { "type": "ephemeral" }
},
{
"type": "text",
"text": "Explain the routing logic in main.rs"
}
]
}
]
}The ephemeral type tells Anthropic to store this prefix. If the next request starts with the same "entire codebase" text, the latency will drop from seconds to milliseconds.
The Trade-offs
Prompt caching is not a silver bullet. There are three primary constraints to consider:
- Exact Match Requirement: Even a single character change in the cached prefix invalidates the entire cache. You cannot change a variable name in the middle of a cached block and expect the rest of the block to remain cached.
- Minimum Token Threshold: Caching is typically only available for prompts over a certain length (e.g., 1,024 tokens). For small scripts, the overhead of managing cache breakpoints exceeds the benefit.
- Cost Shift: You trade a higher initial "write" cost for lower subsequent "read" costs. This is ideal for iterative coding sessions but less effective for single-shot queries.
Conclusion
Prompt caching shifts the economics of long-context LLMs. By reducing the cost of repeat inputs, it enables a more fluid interaction between the developer and the agent. Whether you are using a lightweight native client like AZMX AI or building your own orchestration layer, leveraging cache_control is the most effective way to scale your AI's context window without scaling your monthly API bill. For those prioritizing privacy and security, remember that AZMX AI maintains a strict deny-list for .env and .ssh files, ensuring that your cached context never accidentally includes your private credentials. Check out our security page for more details.