Zum Hauptinhalt springen
LIVE
CVSS 7.0CVE-2026-138158 | libgphoto2 through 2.5.34 contains a stack buffer overflow in sierra_get_string_register() that lets a malicious sierra-protocol camera or USB device overwrite stack memory. When files are listed, the device can answer the file-name register with an oversized packet, writing up to 32 KB past small stack buffers to cause crashes or possible code execution.CVSS 7.7CVE-2026-138159 | edbrowse through 3.8.19 contains an integer overflow in stringAndBytes() that allows malicious servers to write past a heap buffer by sending response bodies larger than 2 GB. When a user browses to an attacker-controlled HTTP, FTP, or gopher server, the oversized body skips reallocation and corrupts the heap, causing crashes or potentially code execution.CVSS 8.5CVE-2026-138160 | Gammu before 1.45.2 contains a stack-based buffer overflow vulnerability in ReadBackupTextLen() that allows attackers to overwrite stack memory via crafted backup files. Attackers can supply overlong quoted text values decoded into a 10,000-byte stack array during convertbackup, restore, or GSM_ReadBackupFile calls, causing crashes or potential code execution.CVSS 8.5CVE-2026-138161 | Gammu before 1.45.2 contains a buffer overflow vulnerability in LoadBackup() that allows attackers to corrupt memory by supplying an overlong MD5 value in a crafted backup file. Attackers can overwrite adjacent phonebook and calendar entry pointers via strcpy() into MD5Original when files are loaded with convertbackup or restore, causing crashes or potential code execution.CVSS 7.3CVE-2026-138162 | Gammu before 1.45.2 contains a stack-based buffer overflow vulnerability in ALCATEL_EncodeString() that allows attackers to overwrite stack memory via oversized encoded phonebook text. Attackers can supply a crafted backup restored with gammu restore or addnew to an attached Alcatel phone, using GSM extension characters to crash Gammu or potentially execute code.CVSS 8.5CVE-2026-138163 | Gammu before 1.45.2 contains a heap-based buffer overflow in ReadWAPSettingsEntry() that allows attackers to corrupt heap memory via crafted backup files with excess WAP or MMS settings entries. Attackers can supply a backup with more than four TitleNN keys in a [WAPSettings] or [MMSSettings] section, loaded via convertbackup, restore, or GSM_ReadBackupFile, causing crashes or potential code execution.CVSS 4.8CVE-2026-138164 | Gammu before 1.45.2 contains a stack-based buffer overflow vulnerability in SaveBackupText() in backtext.c that allows attackers to corrupt stack memory by supplying long text fields. Attackers can craft a backup with long calendar or note fields that, when converted with gammu convertbackup, writes hex digits past the 10,000-byte stack buffer.CVSS 9.3CVE-2026-138154 | POSTAL 2 before build 26810491 contains a sandbox escape vulnerability that allows malicious Steam Workshop mods to execute code by calling ConsoleCommand with a start file URI. Attackers can ship a batch file in a mod at the predictable workshop install path and launch it via ShellExecuteW, executing commands as the player on Windows.
Alle im Drawer
Intelligence View
⚡ tsecurity.de Intelligence

Prompt Caching and Cost Control in Python

Introduction https://pg-blogs.netlify.app/posts/10-building-reliable-llm-apps-in-python/ closed with a section on picking the right model per task and caching…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Quelle & Fakten · 11 Min. Lesezeit
Quelle: dev.to
Rubrik: Secure Programming




Introduction



https://pg-blogs.netlify.app/posts/10-building-reliable-llm-apps-in-python/ closed with a section on picking the right model per task and caching a shared prefix. That was the entry point into a bigger discipline: LLM spend is an engineering variable, not a fixed bill — one you can measure and reduce with the same rigor you'd apply to query latency or memory footprint.



This post goes deeper on four levers: how input/output pricing actually works and why the prefix is usually where the money goes, the exact cache_control shape and how to prove a cache hit instead of assuming one, the Batches API for work that isn't latency-sensitive, and model routing — a cheap model triaging requests and escalating only the hard ones. The throughline is honest: measure before you optimize. Every lever here has its own cost; misapplied, it makes things slower or pricier, not cheaper.









Token Economics: Why the Prefix Is the Bill



LLM providers price input and output tokens separately, and output always costs more — generation is autoregressive (each token depends on every one before it), while input can be processed in parallel. Representative pricing from the current model catalog:




























Model Input Output
Claude Opus 4.8 $5.00 / MTok $25.00 / MTok
Claude Sonnet 5 $3.00 / MTok $15.00 / MTok
Claude Haiku 4.5 $1.00 / MTok $5.00 / MTok


Two things follow:





  1. A long system prompt, tool list, or RAG context is billed as input on every request, not written once. Send a 20K-token system prompt on 10,000 requests and that's 200M input tokens — at Opus 4.8 rates, $1,000 before the model has generated a single output token. The shared prefix, not the user's actual question, is usually the dominant cost.


  2. Verbose output costs twice — once directly (more output tokens billed at the higher rate), and again because the next turn's history carries that verbosity forward as input. Asking for concise output and setting a sane max_tokens is a cost control, not just a style choice.



This is why the two techniques below — caching the stable prefix, and not running every request through the most expensive model — are the highest-leverage levers, in that order.









Prompt Caching: Pay to Write Once, Read Cheaply



Prompt caching marks a stable prefix of a request — a system prompt, tool definitions, retrieved RAG context — so a later request with an identical prefix reads it back cheaply instead of reprocessing it. Grounded in the bundled claude-api skill's shared/prompt-caching.md:





  • It's a prefix match. The cache key derives from the exact bytes up to each cache_control breakpoint. One byte different anywhere in the prefix — an interpolated timestamp, a reordered JSON key, a reordered tool list — invalidates everything downstream of it.


  • Render order is tools → system → messages. A breakpoint on the last system block caches tools and system together.


  • Cache reads cost ~0.1× base input price; cache writes cost 1.25× (5-minute TTL) or 2× (1-hour TTL). At a 5-minute TTL, two requests already break even (1.25× + 0.1× vs. 2× uncached); a 1-hour TTL needs roughly three requests to pay off the higher write cost.


  • The minimum cacheable prefix is model-dependent — Opus 4.8 needs at least 4,096 tokens. Below that, cache_control silently does nothing: no error, just cache_creation_input_tokens: 0.






The exact shape






import anthropic

client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment

# STABLE_SYSTEM_PROMPT holds the tool docs / policy text / retrieved corpus —
# large, and byte-identical across many requests.
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system=[{
"type": "text",
"text": STABLE_SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"}, # default TTL: 5 minutes
}],
messages=[{"role": "user", "content": user_question}], # varies — no marker here
)






For a prefix reused across bursty traffic with gaps longer than five minutes, pass an explicit 1-hour TTL instead:




"cache_control": {"type": "ephemeral", "ttl": "1h"}









Confirming a cache hit — don't assume, check the field



The response usage object is the ground truth. input_tokens reports only the uncached remainder — total prompt size is the sum of all three fields:




usage = response.usage
print("cache write:", usage.cache_creation_input_tokens) # paid ~1.25x
print("cache read: ", usage.cache_read_input_tokens) # paid ~0.1x
print("uncached: ", usage.input_tokens) # paid full price






The first call against a new prefix shows cache_creation_input_tokens > 0 and cache_read_input_tokens == 0 — that request paid the write premium. Every subsequent call with the same prefix, inside the TTL, should show cache_read_input_tokens > 0 and cache_creation_input_tokens == 0. If cache_read_input_tokens stays zero across repeated, apparently-identical requests, something in the prefix is silently different — datetime.now() baked into the system prompt, a uuid4() near the front, json.dumps(d) without sort_keys=True, or a tool list assembled from an unordered set. Diff the rendered prompt bytes between two calls to find it before concluding caching "doesn't help here."



Architectural rule that matters more than marker placement: don't change the tool list or model mid-session — both render at the front of the request and invalidate the whole prefix on any change. Don't interpolate a timestamp or user ID into the system prompt; push anything that varies per request to the end of messages, after the last cache_control marker.









The Batches API: Half Price When Latency Doesn't Matter



Not every call needs a response in two seconds. Nightly report summarization, bulk document classification, backfilling embeddings metadata, re-scoring an eval set — none are latency-sensitive, and all are exactly the workload the Message Batches API is priced for: 50% off standard token pricing, in exchange for asynchronous processing (most batches finish within an hour; the hard ceiling is 24 hours; results stay available for 29 days).



Grounded in the bundled claude-api skill's python/claude-api/batches.md. Submit a list of requests, each with a custom_id you choose to match results back later:




import anthropic
import time
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request

client = anthropic.Anthropic()

items_to_classify = [
"The product quality is excellent!",
"Terrible customer service, never again.",
"It's okay, nothing special.",
]

batch = client.messages.batches.create(
requests=[
Request(
custom_id=f"classify-{i}",
params=MessageCreateParamsNonStreaming(
model="claude-haiku-4-5", # cheap model — a simple classification task
max_tokens=50,
messages=[{
"role": "user",
"content": f"Classify as positive/negative/neutral (one word): {text}",
}],
),
)
for i, text in enumerate(items_to_classify)
]
)
print(f"Created batch: {batch.id}")

# Poll until done — most batches finish well under an hour
while True:
batch = client.messages.batches.retrieve(batch.id)
if batch.processing_status == "ended":
break
time.sleep(10)

# Stream results, matched back to custom_id
results = {}
for result in client.messages.batches.results(batch.id):
match result.result.type:
case "succeeded":
msg = result.result.message
results[result.custom_id] = next(
(b.text for b in msg.content if b.type == "text"), ""
)
case "errored":
print(f"[{result.custom_id}] error — fix and retry, or safe to retry if server error")
case "expired" | "canceled":
print(f"[{result.custom_id}] did not complete — resubmit if still needed")

for custom_id, label in sorted(results.items()):
print(f"{custom_id}: {label}")






Every Messages API feature — including prompt caching — works inside a batch. A batch of 10,000 classification calls that all share one large system prompt gets both the 50% batch discount and the cache-read discount on the shared prefix, applied independently:




shared_system = [
{"type": "text", "text": "You are a support-ticket classifier."},
{"type": "text", "text": large_policy_document, "cache_control": {"type": "ephemeral"}},
]

batch = client.messages.batches.create(
requests=[
Request(
custom_id=f"ticket-{i}",
params=MessageCreateParamsNonStreaming(
model="claude-haiku-4-5",
max_tokens=50,
system=shared_system,
messages=[{"role": "user", "content": ticket_text}],
),
)
for i, ticket_text in enumerate(tickets)
]
)






The cost tradeoff to weigh honestly: batching trades latency for a guaranteed 50% discount plus operational complexity — submit, poll (or wait for a webhook), reconcile by custom_id, and handle errored/expired items that a synchronous call never has to. It's a clear win for bulk, non-interactive work, and the wrong tool the moment a human is waiting on the response. Confirm the workload is actually bulk and actually not latency-sensitive — with real traffic data — before reaching for it.









Model Routing: Let the Cheap Model Triage



The other lever, orthogonal to caching and batching, is not sending every request to your most expensive model. A large share of real traffic — sentiment classification, intent detection, "is this ticket urgent," simple extraction — is well within a cheap model's competence. Route those to claude-haiku-4-5 and reserve claude-opus-4-8 for requests that actually need it.



A simple, honest pattern: run the cheap model first, have it flag its own uncertainty, and escalate only when it does.




def triage_with_haiku(ticket_text: str) -> tuple[str, bool]:
response = client.messages.create(
model="claude-haiku-4-5",
max_tokens=100,
messages=[{
"role": "user",
"content": f"""Classify this support ticket as billing/technical/other.
If you are not confident, say so explicitly.

Ticket:
{ticket_text}

Respond as: <label>|<confident:yes/no>
""",
}],
)
text = next((b.text for b in response.content if b.type == "text"), "other|no")
label, _, confident_flag = text.partition("|")
return label.strip(), confident_flag.strip().lower() == "yes"


def handle_ticket(ticket_text: str) -> str:
label, confident = triage_with_haiku(ticket_text)
if confident:
return label # cheap model handled it — done

# Escalate only the uncertain fraction to the stronger model
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=500,
messages=[{"role": "user", "content": f"Carefully classify and explain this ticket: {ticket_text}"}],
)
return next((b.text for b in response.content if b.type == "text"), "other")






If 80% of tickets are confidently triaged by Haiku at $1/$5 per MTok, and only 20% escalate to Opus at $5/$25, the blended cost is a fraction of routing everything through Opus — with no quality loss on the easy majority, because the escalation path exists exactly for the cases where the cheap model says "I'm not sure." The failure mode to guard against is a cheap model that's overconfident: measure the triage's accuracy against a labeled sample before trusting the split, and tune the confidence threshold from that measurement, not from vibes.









Measure Before You Optimize



Every technique above has a cost of its own — a cache write premium, batch operational overhead, an extra triage call before the "real" one. Applied blindly, any of them can make a system more expensive:




  • Caching a prefix that changes every request pays the write premium with zero reads — worse than not caching.

  • Batching latency-sensitive traffic breaks the product; a 50% saving doesn't matter if a user is staring at a spinner.

  • Routing to a cheap model without measuring its accuracy on your real traffic distribution can silently degrade quality while looking like a cost win on the invoice.



Instrument first: log cache_read_input_tokens / cache_creation_input_tokens / input_tokens per request, track cost per request type, and know your actual latency requirements before reaching for any of these levers. Optimize the workload that's actually expensive — not the one you assume is.









Practical Checklist








































Practice Why it matters
Put the stable prefix (system prompt, tools, RAG context) first, volatile content last Prefix match — anything after a byte change is uncached
Add cache_control only where the prefix meets the model's minimum token count Below the minimum, it silently writes and never reads
Verify with cache_read_input_tokens, not assumption A silent invalidator produces zero reads with no error
Reach for the Batches API only for non-interactive bulk work 50% discount, but async — wrong tool under a human waiting
Combine batching with caching on shared prefixes Discounts stack; both apply independently
Route easy/high-volume requests to a cheap model, escalate on low confidence Blended cost drops without lowering quality on hard cases
Measure cost and latency per request type before optimizing "Optimizing" the wrong workload can make things worse








Final Thoughts



Cost control for LLM applications isn't a separate discipline from correctness — it's the same engineering discipline applied to a new, expensive kind of external call. The prefix is the bill; caching turns a repeatedly-read prefix into a cheap read; batching turns non-urgent bulk work into a 50%-off job; routing turns "always call the strongest model" into "call the model the task actually needs." None of it replaces measurement — instrument the usage fields, know your latency requirements, and let real numbers, not assumptions, decide which lever to pull.

Information ergänzen Fachhinweis, Update, Korrektur oder Quelle zur Meldung beitragen
Gäste-Hinweise werden kurz redaktionell geprüft und danach hier angezeigt.

Community-Hinweise zur Meldung

0 freigegeben
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!
Informations-Bewertung:

💬 Community discussion & comments

🌐 Join the main chat

Start the discussion in the Social Chat broadcast stream or submit an expert contribution via the supplement form above.

Community Rating &amp; Social Proof
⭐ Reader Rating
–
Ø / 5.0
No reader ratings yet
Was this article helpful?
0
Submit your rating
Detailed rating (optional)
Content & Quality
Source & Credibility
Freshness
Security Briefing Free · 1-Click Unsubscribe

Intelligence Digest — free

The top security news daily · unsubscribe anytime

Related story clusters & sources (vector AI)

6 Cluster
22 sources
CVE-2026-72088 | Linux Kernel up to 7.2-rc3 hpsa in_reset memory leak (WID-SEC-2026-2852)
VulDB Updates 1 hour ago
21 more sources
VulDB Updates1 hour agoCVE-2026-72089 | Linux Kernel up to 7.2-rc3 accel/ivpu fw_log_from_bo size/header_size memory corruption (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72090 | Linux Kernel up to 7.1.4 amdxdna amdxdna_drm_sync_bo_ioctl handle/driver_priv improper synchronization (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72091 | Linux Kernel up to 7.1.4/7.2-rc3 amdxdna amdxdna_drm_submit_execbuf null pointer dereference (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72093 | Linux Kernel up to 6.18.39/7.1.4/7.2-rc2 amdxdna amdxdna_gem_dmabuf_mmap use after free (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72092 | Linux Kernel up to 6.18.41/7.1.4/7.2-rc3 amdxdna amdxdna_cmd_submit null pointer dereference (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72095 | Linux Kernel up to 6.18.39/7.1.4/7.2-rc1 dma-fence dma_fence_dedup_array uninitialized variable (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72094 | Linux Kernel dma-fence kernel/dma/fence.c dma_fence_timeline_name/dma_fence_driver_name null pointer dereference (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72097 | Linux Kernel up to 7.2-rc2 dm-verity dm_verity_loadpin_is_bdev_trusted null pointer dereference (WID-SEC-2026-2852) VulDB Updates1 hour agoCVE-2026-72096 | Linux Kernel up to 7.2-rc2 dm-verity race condition (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2022-30594 | Linux Kernel up to 5.17.1 seccomp PTRACE_SEIZE PT_SUSPEND_SECCOMP permission (EUVD-2022-52436 / Nessus ID 224708) VulDB Updates2 hours agoCVE-2026-72075 | Linux Kernel up to 7.1.4 ims-pcu ims_pcu_reset_device race condition (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72076 | Linux Kernel up to 7.1.4 ims-pcu ims_pcu_irq out-of-bounds (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72077 | Linux Kernel up to 6.6.147/6.12.96/6.18.39/7.1.4 ims-pcu denial of service (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72078 | Linux Kernel up to 7.1.4 ims-pcu usb_fill_int_urb input validation (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72079 | Linux Kernel up to 7.1.4 ims-pcu ims_pcu_disconnect use after free (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72081 | Linux Kernel up to 7.2-rc3 efct efct_dispatch_fcp_cmd allocation of resources (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72082 | Linux Kernel up to 7.2-rc3 efct efct_hw_io_abort reference count (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72085 | Linux Kernel up to 7.2-rc3 scsiback scsiback_do_cmd_fn use after free (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72087 | Linux Kernel up to 7.2-rc3 lpfc lpfc_sli4_driver_resource_setup memory leak (WID-SEC-2026-2852) VulDB Updates2 hours agoCVE-2026-72086 | Linux Kernel up to 7.2-rc3 scsiback scsiback_device_action double free (WID-SEC-2026-2852) DEV Community2 hours agoOpenAI and Cisco Put AI Agents Inside the Linux Kernel
20 sources
Have power at your fingertips with these top power station deals this Black Friday weekend — I've tested some impressive battery systems, here are my top picks from Bluetti, Jackery and more
Latest from TechRadar 28 min. ago
19 more sources
CNET42 min. agoShopping for a Gearhead? These Car Gifts Are Up to 40% Off for Black Friday and Cyber Monday The Verge1 hour agoHere’s what other Verge readers bought during Black Friday Latest from TechRadar1 hour agoLooking at a mini PC for local AI workloads? Black Friday may be over, but there's a great deal than the GMKtec EVO-X3 with 128GB of memory Latest from TechRadar1 hour ago9 essential small business upgrades in the Black Friday sales — professional tech deals for small teams, freelancers, and solopreneurs Latest from TechRadar1 hour agoThe Garmin Forerunner 55 is £99 right now, and it’s the Black Friday deal I recommend Latest from TechRadar1 hour agoIs there a Black Friday Disney+ deal in the UK? Here's what you need to know Latest from TechRadar1 hour agoBlack Friday kitchen deals: huge savings all weekend on Ninja, De'Longhi, Breville, and KitchenAid Latest from Windows Central1 hour agoUpgrade your PC listening experience with the best wireless earbuds for Windows 11 devices this Black Friday Latest from TechRadar2 hours agoMeta's best affordable headset is an even better deal for Black Friday Latest from TechRadar2 hours agoThe best Black Friday Garmin deals still live, picked by a smartwatch tester CNET3 days agoThese Post-Black Friday MacBook Deals Make Apple’s Laptops Up to $500 Off Latest from Windows Central2 hours agoThe GameSir G7 Pro 8K is the only PC controller to buy on Black Friday weekend Latest from TechRadar3 hours agoBlack Friday gaming mouse deals 2026: astounding bargains on wired and wireless mice Latest from Windows Central3 hours agoAdd haptics to your Windows 11 PC with 15% off the Logitech MX Master 4 this Black Friday weekend Latest from TechRadar3 hours agoDell has the fastest mini PC under $2000 this Black Friday, plus 25 free Xbox games Latest from TechRadar3 hours agoBlack Friday vacuum cleaner deals: Shark and Dyson offers running all weekend WIRED3 hours ago17 Best Buy Black Friday Deals on Hot Tech (2026): Up to 65% Off Latest from TechRadar3 hours agoBlack Friday streaming deals from 99 cents: LIVE updates on the best savings for Disney+, Hulu, Paramount+, HBO Max, and more The Verge3 hours agoAlmost every great Black Friday deal is still happening this weekend
Similar posts
🔍 Verwandte News

Auch interessante Nachrichten Prompt Caching and Cost Control in Python

Thematisch verwandte Begriffe: Prompt, Caching, Cost, Control · 6 Treffer

Laden...

Loading posts ...

Laden...

Loading videos ...

Laden...

Loading posts ...

Laden...

Loading videos ...

Laden...

Loading posts ...

Laden...

Loading videos ...

Laden...

Loading posts ...

Laden...

Loading videos ...

💬 Loading comments…
Pull to refresh
Widgets & Topics
Loading widgets…
Nächster Beitrag