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

# Model routing for AI agents: stop overspending on simple tasks

> Agentic pipelines chain dozens of LLM calls. Routing each call to the right model slashes cost without touching quality.

An agentic pipeline might call an LLM 30–50 times per task: planning, tool selection, web browsing,
reasoning, summarization, code review, final synthesis. If all 50 calls hit a frontier model at
$15–21/MTok, a single agent run costs $0.30–\$1.00+.

Most of those calls don't need a frontier model. A call that checks whether a tool invocation
succeeded is a simple classification task. A call that summarizes a fetched web page is a
summarization task. Routing them to budget models reduces cost while keeping quality where it matters.

## Routing inside an agent loop

```python theme={null}
from prysm import Prysm

client = Prysm()

def agent_step(step_type: str, context: str) -> str:
    """Route each agent step to the right model."""

    # Classify which step type we're in
    routing_mode = {
        "plan":        "quality",    # Use the best model for planning
        "reason":      "quality",    # Use the best model for reasoning
        "summarize":   "agility",    # Budget model for summarization
        "classify":    "agility",    # Budget model for classification
        "synthesize":  "balanced",   # Mid-tier for final synthesis
    }.get(step_type, "balanced")

    resp = client.chat.completions.create(
        model="auto",
        messages=[{"role": "user", "content": context}],
        extra_body={"routing_mode": routing_mode},
    )
    return resp.choices[0].message.content
```

## Using AgentGuard for cost-bounded runs

PRYSM's AgentGuard stops agent runs before they exceed a cost ceiling:

```python theme={null}
result = client.agents.run(
    goal="Research and summarize the top 5 papers on retrieval-augmented generation",
    max_cost_usd=0.25,     # Hard stop at $0.25 — never exceeded
    max_steps=15,
)

print(result["output"])
print(f"Steps: {result['steps_taken']}  Cost: ${result['cost_usd']:.4f}")
print(f"PrysmProof: {result['proof']['proof_hash']}")
# Steps: 8  Cost: $0.0847
# PrysmProof: sha256:a1b2c3d4e5f67890
```

AgentGuard tracks cumulative cost across every LLM call in the run. If the next call would
exceed `max_cost_usd`, the agent stops cleanly and returns what it has so far.

## BRAIN.md: declare routing rules for your repo

Create a `BRAIN.md` at your repo root to set routing rules that apply automatically:

```yaml theme={null}
# BRAIN.md
max_cost_per_request: 0.10      # Never spend more than $0.10 on a single call

rules:
  - when: "code"
    model: "claude-sonnet-4.5"  # Code-specialized for coding tasks
  - when: "reasoning"
    model: "deepseek-r1"        # Strongest reasoning model for math/logic
  - when: "summarize"
    routing_mode: agility       # Budget model for summarization

blocked:
  - gpt-5.2-pro               # Too expensive for most tasks
```

```python theme={null}
# BRAIN.md is auto-discovered from the repo root
client = Prysm()  # automatically reads ./BRAIN.md

# All calls now respect the rules above
resp = client.complete("Prove that sqrt(2) is irrational")
# Routed to deepseek-r1 (reasoning rule matched)
```

## Dry-run: preview without spending tokens

```python theme={null}
preview = client.route(
    "Summarize this 2000-word article",
    brain={"max_cost_per_request": 0.10, "rules": [{"when": "summarize", "routing_mode": "agility"}]},
)
print(preview["routing"]["model"])    # mistral-nemo
print(preview["routing"]["why"])      # "summarize rule + agility mode"
print(preview["estimated_cost"]["est_1k_tokens"])  # 0.00002 USD
```

## Measuring savings over a run

```python theme={null}
savings = client.savings(baseline="gpt-5.2-pro")
print(f"Agent spent ${savings['actual_usd']:.4f} vs. ${savings['baseline_usd']:.4f} baseline")
print(f"Saved {savings['saved_pct']}% by routing")
```

The key insight: most agent steps are routine — tool invocations, parsing, formatting, classification.
Only planning and final synthesis need frontier reasoning. Route accordingly.
