CCA-F › Domain 5 › Cost — Anti-Patterns & Best Practices

💰 Cost — Anti-Patterns, Best Practices & the JSON that always costs more

What you actually pay for · every waste pattern & its fix · CoT / adaptive thinking cost · a guardrail checklist
🎯 This page is both a CCA exam reference and the source list for cost guardrails — the numbered RULE boxes are written to be turned into automated checks (e.g. in CostSherlock) later.

1 · THE COST MODEL What you actually pay for

Every /v1/messages call bills input tokens + output tokens, at different rates. Four facts drive almost all cost decisions:

Pricing & cache multipliers (the numbers to know)

ModelInput $/1MOutput $/1MOutput ÷ Input
Claude Opus 4.8 / 4.7$5$255×
Claude Sonnet 5$3$155×
Claude Haiku 4.5$1$55×
Claude Fable 5$10$505×
Token classMultiplier vs base inputusage field
Normal input1×input_tokens
Cache read (hit)~0.1× — ~90% cheapercache_read_input_tokens
Cache write, 5-min TTL1.25×cache_creation_input_tokens
Cache write, 1-hour TTL2×cache_creation_input_tokens
Output (incl. thinking)5× (per table above)output_tokens
Batch API~0.5× on input and output—
A cached prefix breaks even after just two requests: 1.25× + 0.1× = 1.35× vs 2× uncached. Cache read is the single biggest lever on a repeated prefix. (Full mechanics: Prompt Caching & Memory.)

2 · COST ANTI-PATTERNS The ways teams overspend

#Anti-patternWhy it costsFix
A1No prompt caching on a stable system prompt / tools / schemaThe whole prefix is re-processed at 1× every callAdd cache_control on the last stable block
A2Cache-busting the prefix — a timestamp, UUID, or unsorted JSON inside the cached regionPrefix bytes change → miss → you pay the write premium and full input, every callMove volatile data after the breakpoint; sort JSON keys
A3Carrying huge tool_result payloads in history foreverStateless resend re-bills them every turnContext editing (clear old tool uses) · compaction
A4Over-large model for the task (Opus for classification)Paying 5× Haiku's rate for no quality gainModel-tier by task; escalate only hard steps
A5Unbounded agent loop (no max_turns)A stuck agent calls tools foreverHard turn cap + graceful stop
A6Huge max_tokens / no output ceilingLets a response run long at 5× rateSet the smallest max_tokens that fits
A7Thinking / high effort on trivial tasksCoT tokens billed as output for no benefitLow effort (or no thinking) for easy work
A8Prose when you need dataYou pay output tokens for framing wordsoutput_config.format + a tight schema
A9One request per item for bulk workNo batch discount, more overheadBatch API (~50% off) for latency-tolerant jobs
A10Over-fetching context (whole schema / all docs)Input tokens for content the task never needsRAG / retrieve only what's relevant; slim tool defs
A11Cache breakpoint after the volatile partNothing stable precedes it → never cachesOrder tools→system→messages; breakpoint after the big stable chunk
A12Letting the cache expire between calls (default 5-min TTL, sparse traffic)Every call is a fresh write, never a read1-hour TTL if reuse is spaced; keep it warm
A13No streaming on long outputsRequest timeout → retry → pay twiceStream; use get_final_message()
A14Duplicating the system prompt into each user turnRe-billed as message input every turn, uncacheable therePut durable instructions in system once, and cache it
A15Changing tools/system mid-conversationInvalidates the whole downstream cache (cascade)Keep tools/system byte-stable across the session
The compounding trap: A2 + A3 together — a cache that never hits and a history that keeps growing — means every turn re-processes a bigger prefix at full price. This is the most common runaway-cost bug in agents.

3 · MESSAGE JSON THAT ALWAYS COSTS MORE Same result, higher bill

These request shapes are strictly more expensive than an equivalent that returns the same answer. Each pair is ❌ costlier vs ✅ cheaper.

3.1 · Volatile data inside the cached prefix → cache never hits (A2)

❌ A timestamp sits in the cached system block, so its bytes change every call — permanent miss + write premium.
"system": [{
  "type":"text",
  "text":"Current time: 2026-07-16T09:31:07Z. You are a support agent. <5000-token policy…>",
  "cache_control":{"type":"ephemeral"}          # caches a prefix that changes every call → useless
}]
✅ Static policy cached; the volatile time moves into the (uncached) user turn.
"system": [{
  "type":"text", "text":"You are a support agent. <5000-token policy…>",
  "cache_control":{"type":"ephemeral"}          # byte-stable → HIT at ~0.1× after the first call
}],
"messages":[{"role":"user","content":"[time: 2026-07-16T09:31:07Z] …"}]

3.2 · Big stable content with no cache_control → re-billed at 1× forever (A1)

❌ A large schema/tools re-sent every turn, uncached — full input price on every call.
"tools": [ …40 tool defs… ],
"system": "…8K-token schema…"            # no cache_control anywhere → 1× every single turn
✅ Same content, one breakpoint after the stable region → ~0.1× on every later turn.
"tools": [ …40 tool defs… ],
"system": [{"type":"text","text":"…8K-token schema…",
             "cache_control":{"type":"ephemeral"}}]   # covers tools+system (order: tools→system→messages)

3.3 · Huge tool_result kept in history → re-billed every turn (A3)

❌ A 30K-token API dump stays in messages for the rest of the session — paid again on every subsequent call.
"messages":[ …, {"role":"user","content":[{"type":"tool_result",
   "tool_use_id":"t1","content":"<30K tokens of raw JSON>"}]}, … ]
✅ Clear stale tool results once they're no longer needed (context editing) — the dialogue stays intact.
# request context_management with clear_tool_uses_20250919 → old tool_results dropped,
# conversation preserved. See the context-bloat cheat sheet.

3.4 · Oversized ceiling + thinking on a yes/no task (A6/A7)

❌ A trivial classification with room to ramble and forced deep thinking — output (incl. CoT) billed at 5×.
{"model":"claude-opus-4-8", "max_tokens":32000,
 "output_config":{"effort":"high"}, "messages":[{…"Is this spam? yes/no"}]}
✅ Small model, tight ceiling, low effort, structured answer.
{"model":"claude-haiku-4-5", "max_tokens":8,
 "output_config":{"effort":"low",
   "format":{"type":"json_schema","schema":{"type":"object",
     "properties":{"spam":{"type":"boolean"}},"required":["spam"]}}}}

3.5 · One call per item vs one batch (A9)

❌ 10,000 synchronous calls, full price, plus per-request overhead.
✅ One Message Batches job — ~50% off input and output, correlate results by custom_id. Use it whenever latency isn't critical.

4 · CoT & ADAPTIVE THINKING The expensive tokens

Extended thinking (chain-of-thought) is where cost quietly grows, because thinking tokens are billed as output (5×). Key facts for the exam and for guardrails:

Exam trap: "raise the thinking budget to make it smarter/cheaper." Wrong on both — budget_tokens is removed (400) on current models, and more thinking is more output cost. Use adaptive thinking + the effort dial, and only where reasoning is actually needed.

5 · COST BEST PRACTICES The mirror image

6 · COST GUARDRAIL CHECKLIST The CostSherlock seed

Written as rules you can automate against a request payload + its usage response:

R1 · Uncached big prefix: IF len(system)+len(tools) > min_prefix(model) AND no cache_control present → WARN (add caching).
R2 · Cache miss on a stable prefix: IF prefix unchanged since last call AND cache_read_input_tokens == 0 → FLAG a cache-buster (volatile field / unsorted JSON / breakpoint placement).
R3 · Oversized model: IF task class ∈ {classify, extract, route} AND model is Opus/Fable → SUGGEST Haiku/Sonnet.
R4 · No output ceiling: IF max_tokens unset or > task budget → WARN.
R5 · Effort mismatch: IF effort ∈ {high,xhigh,max} on a task class that doesn't need reasoning → SUGGEST lower effort.
R6 · History bloat: IF any single tool_result > N tokens AND it persists across > K turns → SUGGEST context editing.
R7 · Prose where data is wanted: IF downstream parses the output AND no output_config.format → SUGGEST structured output.
R8 · Unbatched bulk: IF request rate is high AND latency SLA is loose AND not using Batch → SUGGEST Batch API.
R9 · Unbounded loop: IF agent loop has no max_turns → BLOCK.
R10 · Deprecated params: IF budget_tokens / temperature / top_p present on a current model → BLOCK (400 anyway).

7 · EXAM ONE-LINERS

• Output ≈ 5× input, and thinking counts as output — verbose reasoning is the expensive path.
• The API is stateless: every token in messages is re-billed every turn — trim history.
• Cache read ~0.1×, write 1.25× (5-min) / 2× (1-hour); break-even at 2 requests.
• Batch API ~50% off input and output for latency-tolerant work.
• Control thinking with adaptive + effort, never budget_tokens (400 on current models).
• The classic runaway bug: a cache that never hits + a history that never shrinks.