prompt_caching=True and Swarms marks the stable prefix so the provider reuses it — you pay full price once, then a discount on every call after.
When to use it
- A large, reusable system prompt (persona, policies, examples) sent on every call.
- The same agent runs many times or holds a multi-turn conversation.
- Tool-heavy agents whose tool schemas stay constant across calls.
- Long context (docs, transcripts) reused turn after turn.
Basic usage
Flip onprompt_caching. On Anthropic, Swarms adds cache_control breakpoints to the stable prefix; on OpenAI, caching is automatic and the flag leaves messages untouched. Caching only kicks in above the provider’s token minimum (see the table in the guide), so the system prompt must be large — here we repeat a string to cross that bar.
prompt_caching=Truemarks the system prompt (plus the last message) as cacheable.- The first
runpays to write the cache. - Every later
runreads the cached prefix instead of re-billing it.
Tune it with cache_config
Pass acache_config dict to control caching. The common knob is ttl — Anthropic supports a 1-hour cache:
cache_config also accepts cache_system_prompt, cache_messages, cache_tools, override, and OpenAI’s prompt_cache_key / prompt_cache_retention. See the full Prompt Caching guide under Agent Development for the complete list.
Verify it worked
agent.usage adds up the provider’s token counts for every call the agent makes. Its cached_tokens key is the part of input_tokens served from the cache. Snapshot it around the second run:
See also
- Prompt Caching — Full reference: all
cache_configkeys, provider behavior, and cost details. - Context Compression — Shrink long transcripts before they hit the context limit.