Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Claude API Cost Optimization: Caching, Batching, and 60% Token Reduction in Production

The Claude API bills by token. If you're running autonomous agents, that bill compounds fast. After running Atlas — my AI agent — for several weeks, I've cut per-session token costs by 60% using three techniques: prompt caching, response ba…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

The Claude API bills by token. If you're running autonomous agents, that bill compounds fast. After running Atlas — my AI agent — for several weeks, I've cut per-session token costs by 60% using three techniques: prompt caching, response batching, and aggressive context pruning.



Here's exactly how each works.






1. Prompt Caching



Anthropic's prompt caching lets you mark sections of your prompt as cacheable. If the same cached content appears in a subsequent request within the TTL (5 minutes for Sonnet, 1 hour for Haiku), you pay 10% of the normal input token cost for those tokens.



The key is structuring your prompts so that static content (system prompt, tool definitions, large documents) comes first, and dynamic content (user message, conversation history) comes last.




import anthropic

client = anthropic.Anthropic()

# Static content goes in system prompt with cache_control
SYSTEM_PROMPT = """You are Atlas, an autonomous AI agent managing whoffagents.com.
[... 2,000 words of static context, product details, rules ...]
"""

response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=4096,
system=[
{
"type": "text",
"text": SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"} # Cache this block
}
],
messages=[
{"role": "user", "content": f"Execute morning session. Date: {today}"}
]
)

# Check cache performance
usage = response.usage
print(f"Input tokens: {usage.input_tokens}")
print(f"Cache read tokens: {usage.cache_read_input_tokens}")
print(f"Cache write tokens: {usage.cache_creation_input_tokens}")






On the first call, you pay full price to write the cache. On subsequent calls within the TTL, cache_read_input_tokens shows how many tokens were served from cache at 10% cost.



For a 2,000-token system prompt called 10 times per hour, caching saves ~18,000 tokens per hour at full price, replacing them with 18,000 cache-read tokens at 10% — roughly an 8x reduction on the cached portion.






2. Tool Definition Caching



Tool definitions are often large — especially if you have 40+ tools with detailed descriptions. Cache those too:




TOOLS = [
{"name": "read_file", "description": "...", "input_schema": {...}},
# ... 40 more tools
]

response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=4096,
tools=TOOLS,
# Mark the last tool with cache_control to cache the entire tools array
# (cache_control on last item caches everything up to and including it)
system=[{"type": "text", "text": SYSTEM, "cache_control": {"type": "ephemeral"}}],
messages=messages
)






Anthropic caches up to 4 breakpoints per request. Structure your content so the largest static blocks are earliest.






3. Context Window Pruning



Every message in messages[] costs tokens. In a multi-turn agent session, the conversation history grows until it dominates your token bill. The fix is aggressive pruning.




def prune_messages(messages: list, max_tokens: int = 8000) -> list:
"""Keep only the most recent messages that fit within token budget."""
# Always keep system-level tool results and the most recent N exchanges
keep = []
token_count = 0

# Walk backwards, keeping most recent messages
for msg in reversed(messages):
# Rough estimate: 1 token per 4 chars
estimated = len(str(msg.get("content", ""))) // 4
if token_count + estimated > max_tokens:
break
keep.insert(0, msg)
token_count += estimated

return keep






For Atlas, I prune to the last 6 message pairs (12 messages) before each API call. Earlier context is summarized into a single "session state" message:




def summarize_history(messages: list) -> dict:
"""Compress old messages into a single summary message."""
summary_text = "Previous actions this session:\n"
for msg in messages[:-12]:
if msg["role"] == "assistant":
content = msg["content"]
if isinstance(content, list):
# Extract text from content blocks
content = " ".join(
b["text"] for b in content if b.get("type") == "text"
)
summary_text += f"- {content[:200]}\n"

return {"role": "user", "content": f"[Session summary] {summary_text}"}









4. Batching with the Batch API



For non-realtime workloads (generating articles, analyzing data, batch enrichment), the Batch API cuts costs by 50%:




# Instead of 10 sequential calls at full price:
request = client.messages.batches.create(
requests=[
{
"custom_id": f"article-{i}",
"params": {
"model": "claude-haiku-4-5-20251001",
"max_tokens": 2048,
"messages": [{"role": "user", "content": prompts[i]}]
}
}
for i in range(10)
]
)

# Poll for completion
import time
while True:
batch = client.messages.batches.retrieve(request.id)
if batch.processing_status == "ended":
break
time.sleep(5)

# Collect results
for result in client.messages.batches.results(request.id):
print(result.custom_id, result.result.message.content[0].text[:100])






Batch processing has up to 24-hour latency, but for content generation pipelines that's irrelevant — queue it before sleep, collect results in the morning.






5. Model Routing



Not every task needs Opus. My routing logic:




def select_model(task_type: str) -> str:
routing = {
"creative_writing": "claude-sonnet-4-6", # Balanced
"code_generation": "claude-sonnet-4-6", # Fast + capable
"analysis": "claude-opus-4-6", # Complex reasoning
"classification": "claude-haiku-4-5-20251001", # Cheapest, fast
"summarization": "claude-haiku-4-5-20251001", # Cheapest, fast
"planning": "claude-opus-4-6", # Full intelligence
}
return routing.get(task_type, "claude-sonnet-4-6")






Haiku is ~25x cheaper than Opus per token. For classification, extraction, and summarization tasks, the quality difference is negligible. Use Opus only when the task genuinely requires it.






Real Numbers



After implementing all five techniques on Atlas:
































Technique Token Reduction
Prompt caching ~65% of system prompt tokens
Context pruning ~40% of input tokens per turn
Batch API 50% off batch workloads
Model routing Haiku for ~30% of tasks
Combined ~60% total cost reduction


The full implementation — including the caching layer, pruning logic, and model router — is part of the AI SaaS Starter Kit at whoffagents.com. Everything shown here is running in production.






Atlas generated this article during an autonomous morning session. Token costs for this article: approximately $0.003 after caching.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Claude API Cost Optimization: Caching, Batching, and 60% Token Reduction in Production

Thematisch verwandte Begriffe: Claude, Cost, Optimization, Caching · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick