prompt_caching=True, and tune it with an optional cache_config dictionary.
Caching is a prefix match. Everything up to a cache breakpoint must be byte-for-byte identical between requests for a cache hit. Keep volatile content (timestamps, per-request IDs, changing tool sets) out of the cached prefix.
Quick start
The minimal setup: flipprompt_caching=True on an Anthropic model. Swarms automatically inserts ephemeral cache_control breakpoints on the tool block, the system prompt, and the last message.
How it works
Prompt caching works on the stable prefix of a request. A cache breakpoint marks the end of a cacheable span; on the next request the provider compares your prefix against what it stored and, if it matches exactly up to a breakpoint, serves everything before that point from cache. The request is assembled in this order, which is also the order things get cached:- Tool definitions (rendered before the system prompt)
- System prompt
- Conversation messages (through the last message)
Which providers use cache_control
Swarms only injects cache_control breakpoints for the Anthropic model family — any model whose name contains claude or anthropic. This covers Claude on the Anthropic API, on AWS Bedrock, and on Google Vertex AI.
Anthropic (Claude)
Requires explicit
cache_control markers. Swarms injects them automatically. Up to 4 breakpoints per request — the defaults use tools + system + last message = 3.OpenAI & xAI
Cache automatically — no markers needed. Swarms leaves their messages untouched. OpenAI-only routing/retention hints are passed through.
Gemini / Google AI Studio
Uses a separate context-cache API, not
cache_control. Injecting markers breaks the request, so Swarms leaves it untouched unless you force override.Others
Left untouched by default. Use
cache_config={"override": True} to force marker injection at your own risk.Anthropic allows a maximum of 4 cache breakpoints per request. The Swarms defaults place breakpoints on the tools block, the system prompt, and the last message (3 total), leaving one spare.
Parameters
Prompt caching is controlled by twoAgent constructor parameters.
Master on-switch. When
True, Swarms adds ephemeral cache_control breakpoints to the stable prefix of each request (for Anthropic models) so the prefix is cached and re-billed at a discount. When False, no caching behavior is added and cache_config is ignored.cache_config is a dictionary of fine-grained options. It is only consulted when prompt_caching=True. Every key is optional and falls back to the defaults below.
Cache lifetime.
"5m" (default) or "1h" for Anthropic’s extended one-hour cache. The 1-hour cache costs 2x on writes but survives longer gaps between requests. The required beta header (extended-cache-ttl-2025-04-11) is attached automatically when you select "1h".Cache the system-prompt prefix. Turn off if your system prompt is small or changes every request.
Cache through the last message, enabling incremental multi-turn caching where each new turn extends the cached prefix.
Cache the tool-definitions block. Tools render before the system prompt, so this is a big win for tool-heavy agents with large schemas.
Force
cache_control injection on (True) or off (False) regardless of the detected provider. None (default) auto-detects based on the model name. Set to True to try injection on providers Swarms would normally skip (e.g. Gemini).OpenAI-only routing hint. Grouping requests under a stable key raises cache hit rates. Passed through to the provider; ignored for Anthropic.
OpenAI-only cache TTL:
"in_memory" (default) or "24h". Passed through to the provider; ignored for Anthropic.Default caching
Withprompt_caching=True and no cache_config, you get the sensible defaults: a 5-minute TTL caching tools, system prompt, and the last message.
ttl="1h" — extended one-hour cache
Use the one-hour cache when there are longer gaps between related requests. Writes cost 2x, but the prefix survives well past the default 5-minute window. The beta header is attached for you.
Toggling cache_system_prompt and cache_messages
You can cache the system prompt but not the running conversation (or vice versa). This is useful when the system prompt is large and stable but each turn’s content varies enough that message caching would rarely hit.
cache_tools — caching the tool block
Tool definitions render before the system prompt, so caching them is a big win for tool-heavy agents with large JSON schemas. This example attaches a schema via tools_list_dictionary (a plain schema, no callable) so the tool block itself is large and stable.
override — forcing injection on Gemini
By default Swarms skips cache_control for Gemini because it uses a different caching API. Set override=True to force marker injection anyway if you want to experiment.
OpenAI passthrough — prompt_cache_key and prompt_cache_retention
OpenAI caches automatically, so Swarms doesn’t inject markers. But it does pass through OpenAI’s routing and retention hints. Use a stable prompt_cache_key to raise hit rates and prompt_cache_retention to extend the TTL to 24 hours.
All options — reference config
A single config showing every supported key together.Provider support
Caching is silently skipped below the provider’s minimum cacheable prefix — there is no error, the request simply isn’t cached. Make sure your stable prefix (tools + system prompt + history) exceeds the minimum, and verify with the usage fields described below.| Provider / model | Minimum input tokens |
|---|---|
| OpenAI | 1,024 |
| Anthropic Claude 3.x | 1,024 |
| Anthropic Sonnet / Opus 4.x | 2,048 |
| Anthropic Haiku 4.5+, Opus 4.5+ | 4,096 |
| Google Gemini | 1,024 |
cache_control vs. automatic caching:
- Anthropic (Claude / Bedrock / Vertex) — requires explicit
cache_controlbreakpoints. Swarms injects them automatically when the model name containsclaudeoranthropic. Up to 4 breakpoints per request. - OpenAI and xAI — cache automatically with no markers. Swarms leaves messages untouched and passes through OpenAI’s
prompt_cache_key/prompt_cache_retention. - Gemini / Google AI Studio — uses a separate context-cache API. Markers would break the request, so Swarms leaves it untouched unless you set
cache_config={"override": True}.
Verifying cache hits
Agent.run() returns a formatted string, so it won’t show token usage. To inspect cache behavior, read from the agent’s underlying LLM wrapper directly with return_all = True, which returns the raw provider response including the usage block.
(Anthropic) Tokens written to the cache on this request. Non-zero on the first call that populates a new prefix.
(Anthropic) Tokens read from the cache on this request — this is where the savings show up. Should be non-zero on repeat requests with an identical prefix.
(OpenAI-style) Number of prompt tokens served from cache on this request.
If
cache_read_input_tokens stays 0 across requests that should share a prefix, a silent invalidator is breaking the prefix match — usually a timestamp or UUID in the system prompt, or a changing tool set. Check that everything before the breakpoint is byte-for-byte identical.Gotchas and best practices
- Prefix match is exact. Any byte change before a breakpoint invalidates everything after it. Keep timestamps, per-request IDs, random seeds, and other volatile content out of the system prompt and tool definitions.
- Watch for silent invalidators. Time-enabled conversation history, dynamically reordered tools, or a per-request nonce in the persona will quietly break caching. If
cache_read_input_tokensis always 0, hunt for one of these. - Clear the token minimum. Caching is skipped below the provider minimum with no error. If the prefix is too small, make the system prompt larger or accept that small prompts won’t cache. The examples repeat the persona to exceed the minimum.
temperature=Noneon newer Anthropic models. Opus 4.7 / 4.8 and Sonnet 5 reject atemperaturevalue — passtemperature=Noneon the Agent for those models (as every Anthropic example above does).- The 1-hour cache costs 2x on writes. Use
ttl="1h"only when gaps between related requests exceed the 5-minute default window; otherwise the extra write cost isn’t worth it. - Order matters for tool-heavy agents. Tools render before the system prompt, so a large, stable tool block cached via
cache_tools=Trueis often the single biggest saving. - Stay within 4 breakpoints (Anthropic). The defaults use 3 (tools + system + last message). If you add manual breakpoints elsewhere, keep the total at or below 4.