CachingConfig

Configure caching behavior for agent responses and prompts

Enable response and prompt caching to improve performance and reduce API costs.

Diagram

Caching Flow

Hit

Miss

📝 Request

Cache?

⚡ Return

🤖 Process

💾 Store

Quick Start

  1. Simple Enable

    Enable caching with defaults:

    python
    from praisonaiagents import Agent
    
    agent = Agent(
        name="Cached Agent",
        instructions="Use caching",
        caching=True
    )
  2. With Configuration

    Configure caching behavior:

    python
    from praisonaiagents import Agent
    from praisonaiagents.config import CachingConfig
    
    agent = Agent(
        name="Cached Agent",
        instructions="Use caching",
        caching=CachingConfig(
            enabled=True,
            prompt_caching=True
        )
    )

Configuration Options

python
from praisonaiagents.config import CachingConfig

config = CachingConfig(
    # Response caching
    enabled=True,

    # Prompt caching (provider-specific)
    prompt_caching=None
)
ParameterTypeDefaultDescription
enabledboolTrueEnable response caching
prompt_cachingOptional[bool]NoneOn Anthropic, emits explicit cache_control markers on the system block and the stable history prefix (respecting Anthropic’s 4-breakpoint budget). On OpenAI/Gemini, has no effect at the wire — those providers cache automatically when the prefix is byte-stable.

What gets cached

  • System block: instructions, tools, and memory prelude
  • Stable conversation-history prefix (Anthropic only) — the two most recent messages stay uncached

Common Patterns

Pattern 1: Full Caching

python
from praisonaiagents import Agent
from praisonaiagents.config import CachingConfig

agent = Agent(
    name="Full Cache Agent",
    instructions="Maximum caching",
    caching=CachingConfig(
        enabled=True,
        prompt_caching=True
    )
)

Pattern 2: Auto-Detection with Memory

Even without explicit prompt_caching=True, the SDK auto-detects model support and arranges the prefix accordingly:

python
from praisonaiagents import Agent

agent = Agent(
    name="Auto Cache Agent",
    instructions="Cache optimization happens automatically",
    llm="openai/gpt-4o",      # SDK detects prompt cache support
    memory=True,              # Enables cache-optimized memory context
    caching=CachingConfig()   # prompt_caching=None means auto-detect
)

Pattern 3: Disable Caching

python
from praisonaiagents import Agent
from praisonaiagents.config import CachingConfig

agent = Agent(
    name="No Cache Agent",
    instructions="Always fresh responses",
    caching=CachingConfig(enabled=False)
)

Best Practices

Enable Prompt Caching for Anthropic

Anthropic Claude supports prompt caching for significant cost savings on repeated prompts.

Disable for Real-Time Data

Turn off caching when agents need fresh, real-time information.


Related