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 (Opus 4.5+ needs ~4,096 input tokens), 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.run() returns a formatted string, so to see token usage read from the agent’s own underlying LLM (agent.llm) with return_all = True — that returns the raw response including the usage block. Check the second call for cache_read_input_tokens greater than zero:
cache_read_input_tokens on the second call confirms the cached prefix was reused.
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.