> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompt Cache Optimization

> Stable, cache-friendly prompts across memory, tools, and tasks for lower cost and latency

Turn on memory on a supported model — the SDK sorts tools and lays out context so providers can cache the stable prefix.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent

agent = Agent(
    instructions="You are a helpful assistant",
    llm="openai/gpt-4o",
    memory=True,
)

agent.start("Summarise the latest report")
# Turn 2+ reuses cached system prompt + memory prefix
agent.start("What changed since yesterday?")
```

The user chats across turns; stable prefixes stay cached for lower cost.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "Cache Optimisation Pipeline"
        A[📋 System prompt] --> B[🧠 Memory stable]
        B --> C[🔧 Tools sorted]
        C --> D[⚡ Cache boundary]
        D --> E[💬 User turn]
        E --> F[✅ Cache hit]
    end

    classDef prompt fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef memory fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef tools fill:#F59E0B,stroke:#7C90A0,color:#fff
    classDef boundary fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef variable fill:#10B981,stroke:#7C90A0,color:#fff

    class A prompt
    class B memory
    class C tools
    class D boundary
    class E,F variable
```

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant PromptCacheOptimization

    User->>Agent: Request
    Agent->>PromptCacheOptimization: Process
    PromptCacheOptimization-->>Agent: Result
    Agent-->>User: Response
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Automatic on supported models when memory is enabled:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent

    agent = Agent(
        instructions="You are a helpful assistant",
        llm="openai/gpt-4o",
        memory=True,
    )

    agent.start("Summarise the latest report")
    ```
  </Step>

  <Step title="With Configuration">
    Make prompt caching explicit:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent, CachingConfig

    agent = Agent(
        instructions="You are a helpful assistant",
        llm="anthropic/claude-sonnet-4-20250514",
        memory=True,
        caching=CachingConfig(prompt_caching=True),
    )

    agent.start("Analyse the data trends")
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Feature as Prompt Cache Optimization

    User->>Agent: Request
    Agent->>Feature: Process request
    Feature-->>Agent: Result
    Agent-->>User: Response
```

| Behaviour                | What it does                                                          |
| ------------------------ | --------------------------------------------------------------------- |
| Deterministic tool order | Tools sorted by function name — reordering tools does not break cache |
| Stable memory prefix     | Memory sections in fixed order via `build_cache_optimized_context()`  |
| Cache boundary marker    | Splits stable prefix from variable user turn                          |

Check model support:

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents.llm.model_capabilities import supports_prompt_caching

supports_prompt_caching("openai/gpt-4o")  # True
supports_prompt_caching("ollama/llama3")  # False
```

***

## Configuration Options

| Option           | Type             | Default | Description                                                                    |
| ---------------- | ---------------- | ------- | ------------------------------------------------------------------------------ |
| `enabled`        | `bool`           | `True`  | Response caching                                                               |
| `prompt_caching` | `Optional[bool]` | `None`  | Provider prompt-cache hints (Anthropic manual, OpenAI automatic when eligible) |

Set via `Agent(caching=True)` or `Agent(caching=CachingConfig(...))`.

***

## Best Practices

<AccordionGroup>
  <Accordion title="Use supported models">
    OpenAI, Anthropic, Bedrock, and Deepseek support caching — local models like Ollama do not.
  </Accordion>

  <Accordion title="Keep instructions stable">
    Changing system prompts between turns breaks the cached prefix.
  </Accordion>

  <Accordion title="Enable memory for full optimisation">
    Without memory, only tool sorting applies — `memory=True` activates the cache-optimised context path.
  </Accordion>

  <Accordion title="Let the SDK sort tools">
    Do not manually reorder tool lists — the SDK sorts deterministically by name.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="Prompt Caching" icon="database" href="/docs/features/prompt-caching">
    Deterministic memory ordering for cache hits
  </Card>

  <Card title="Prompt Caching CLI" icon="terminal" href="/docs/cli/prompt-caching">
    Enable caching from the command line
  </Card>
</CardGroup>
