Zum Hauptinhalt springen
LIVE
CVSS 7.4CVE-2026-138376 | AsyncSSH 1.13.1 before 2.24.2 contains an improper authentication vulnerability in SSHConnection._match_known_hosts that accumulates trusted client host keys across host-based authentication attempts on one connection. With trust_client_host enabled, attackers holding one known client host's private key can retry naming another known host and log in as users authorized only from that host.CVSS 7.6CVE-2026-138377 | AsyncSSH 1.13.1 before 2.24.2 contains an improper certificate validation vulnerability that allows remote clients to authenticate as users by abusing X.509 certificates matched in known_client_hosts. Attackers can make a failed host-based authentication attempt with any host key, adding host-only CA certificates to the trust anchors, then authenticate using user certificates issued under those CAs.CVSS 9.1CVE-2026-138378 | react-native-pure-jwt before 3.1.1 on Android contains a signature verification bypass vulnerability that allows unauthenticated attackers to forge tokens because RNPureJwtModule decode() calls JJWT parse() without algorithm pinning. Attackers can craft a token with header alg none and an empty signature to have arbitrary claims accepted as verified, forging identities or roles.CVSS 6.9CVE-2026-138379 | flare-redact before 1.13.1 contains a redaction bypass vulnerability that leaks secrets because the redact() object walker masks sensitive keys only when values are strings. Numbers, objects, arrays and binary data under keys such as password, cvv, pin, cookie or authorization pass through unchanged, exposing secrets in clear text to logger, Sentry, gateway and Fluent Bit sinks.CVSS 6.9CVE-2026-138380 | MediaInfoLib before 26.10 contains a heap-based buffer over-read vulnerability in File_Ogg::Data_Parse() that allows attackers to read beyond heap buffers via crafted Ogg segment sizes. Attackers can supply an Ogg file whose segment sizes exceed the page payload, wrapping the unsigned offset check and causing memcpy over-reads that crash the process or leak heap memory.CVSS 6.9CVE-2026-138381 | GNU LibreDWG 0.14 before 0.15 contains a heap use-after-free vulnerability in in_dxf.c new_object() where the OLE2FRAME oleclient pointer references freed memory after DXF import. Attackers can supply a DXF file with an OLE2FRAME entity to crash dxfwrite or similar applications and leak freed heap contents into exported output.CVSS 10.0CVE-2026-138227 | A vulnerability was found in D-Link R95 1.04B04Beta. This affects the function SetTimeSettings of the file /bin/ssi of the component DHMAPI. Performing a manipulation of the argument TZLocation results in os command injection. Remote exploitation of the attack is possible. The exploit has been made public and could be used.CVSS 10.0CVE-2026-138226 | A vulnerability has been found in D-Link R95 1.04B04Beta. Affected by this issue is the function SetSecureDNSSettings of the file /bin/ssi of the component DHMAPI SOAP interface. Such manipulation of the argument URI leads to os command injection. The attack may be launched remotely. The exploit has been disclosed to the public and may be used.
Alle im Drawer
Intelligence View
⚡ tsecurity.de Intelligence

Prompt Caching and Cost Control in Java

Introduction We already covered picking the right model tier for the task and caching a large shared prefix in…

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




Introduction



We already covered picking the right model tier for the task and caching a large shared prefix in https://pg-blogs.netlify.app/posts/11-building-reliable-llm-apps-in-java/. Those two lines were the tip of a bigger discipline: LLM cost is not a fixed line item, it's an engineering variable — one you can measure and shrink with the same rigor you'd apply to database query time or container memory.



This post goes deeper: how input/output pricing actually works, the exact cache_control shape and how to prove a cache hit rather than assume one, the Batches API for work that isn't latency-sensitive, and model routing — using a cheap model to triage, escalating only the hard cases to a stronger one. The honest framing throughout: measure before you optimize. Every technique here has a cost of its own; applied to the wrong workload, "optimization" makes things slower or more expensive.









Token Economics: Why the Prefix Is the Bill



Anthropic (like every hosted LLM provider) prices input and output tokens separately, and output is always pricier — the model has to generate output autoregressively, one token informed by all the ones 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 consequences follow directly:





  1. Long system prompts, tool definitions, and RAG context are read on every request, not written once. A 20K-token system prompt sent on every one of 10,000 requests is 200M input tokens — at Opus 4.8 rates, $1,000 before a single output token is generated. The shared prefix, not the user's question, is usually where the money goes.


  2. A verbose model wastes money twice — once on the extra output tokens themselves, and again because the next turn's messages history now carries that verbosity forward as input on every subsequent call. Trimming max_tokens and asking for concise output is a cost lever, not just a style preference.



This is why the two techniques below — caching the stable prefix, and not re-running the expensive model on requests a cheap one could handle — are the highest-leverage cost levers available, in that order.









Prompt Caching: Pay to Write Once, Read Cheaply



Prompt caching lets you mark a stable prefix of a request — a system prompt, a tool list, retrieved RAG context — so a subsequent request with an identical prefix reads it back at a fraction of the price instead of reprocessing it. Grounded in the bundled claude-api skill's shared/prompt-caching.md:





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


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


  • Cache reads cost ~0.1× the base input price; cache writes cost 1.25× (5-minute TTL) or 2× (1-hour TTL). With a 5-minute TTL, two requests already break even (1.25× + 0.1× vs. 2×); a 1-hour TTL needs about three.


  • Minimum cacheable prefix is model-dependent — Opus 4.8 needs at least 4,096 tokens. A shorter prefix silently won't cache: no error, just cacheCreationInputTokens() == 0.






The exact shape



The Java SDK carries cache_control on a TextBlockParam. Use .systemOfTextBlockParams(...) — the plain .system(String) overload can't attach cache control:




import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.CacheControlEphemeral;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
import com.anthropic.models.messages.TextBlockParam;

import java.util.List;

AnthropicClient client = AnthropicOkHttpClient.fromEnv(); // reads ANTHROPIC_API_KEY

// STABLE_SYSTEM_PROMPT holds the tool docs / policy text / retrieved corpus —
// large, and byte-identical across many requests.
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.CLAUDE_OPUS_4_8)
.maxTokens(1024L)
.systemOfTextBlockParams(List.of(
TextBlockParam.builder()
.text(STABLE_SYSTEM_PROMPT)
.cacheControl(CacheControlEphemeral.builder().build()) // default TTL: 5 minutes
.build()))
.addUserMessage(userQuestion) // varies per request — no cache_control here
.build();

Message response = client.messages().create(params);






For a longer-lived prefix (a corpus that's reused across a bursty traffic pattern with gaps longer than five minutes), pass an explicit TTL:




TextBlockParam.builder()
.text(STABLE_SYSTEM_PROMPT)
.cacheControl(CacheControlEphemeral.builder()
.ttl(CacheControlEphemeral.Ttl.TTL_1H)
.build())
.build()









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:




var usage = response.usage();
System.out.println("cache write: " + usage.cacheCreationInputTokens()); // paid ~1.25x
System.out.println("cache read: " + usage.cacheReadInputTokens()); // paid ~0.1x
System.out.println("uncached: " + usage.inputTokens()); // paid full price






The first call against a new prefix shows cacheCreationInputTokens() > 0 and cacheReadInputTokens() == 0 — that request paid the write premium. Every subsequent call with the same prefix, inside the TTL, should show cacheReadInputTokens() > 0 and cacheCreationInputTokens() == 0. If cacheReadInputTokens() stays zero across repeated, apparently-identical requests, something in the prefix is silently different — a non-deterministic-order tool list, an interpolated request ID, a system prompt built with a HashMap instead of a LinkedHashMap. Diff the rendered bytes to find it before you conclude caching "doesn't work here."



Architectural rule that matters more than marker placement: don't change the tool list or the model mid-session, and don't interpolate a timestamp or user ID into the system prompt — both sit ahead of the breakpoint and invalidate the whole prefix on every call. 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 LLM call needs a response in two seconds. Nightly report summarization, bulk document classification, backfilling embeddings metadata, re-scoring an eval set — none of these are latency-sensitive, and all of them 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 ceiling is 24 hours).



The bundled skill's grounding for this endpoint (python/claude-api/batches.md) documents the Python and TypeScript shapes fully: submit a list of {custom_id, params} request items via client.messages.batches.create(requests=[...]), poll client.messages.batches.retrieve(batch_id) until processing_status == "ended", then stream client.messages.batches.results(batch_id), matching each result back to its custom_id. Every Messages API feature — including prompt caching — works inside a batch, so 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.



The Java SDK exposes the same endpoint under com.anthropic.models.messages.batches. Each item pairs a customId with its own request params, submitted in one BatchCreateParams:




import com.anthropic.client.AnthropicClient;
import com.anthropic.models.messages.Model;
import com.anthropic.models.messages.batches.BatchCreateParams;
import com.anthropic.models.messages.batches.BatchResultsParams;
import com.anthropic.models.messages.batches.BatchRetrieveParams;
import com.anthropic.models.messages.batches.MessageBatch;
import com.anthropic.models.messages.batches.MessageBatchIndividualResponse;
import com.anthropic.core.http.StreamResponse;

BatchCreateParams params = BatchCreateParams.builder()
.addRequest(BatchCreateParams.Request.builder()
.customId("invoice-42")
.params(BatchCreateParams.Request.Params.builder()
.model(Model.CLAUDE_HAIKU_4_5) // a cheap model is a natural batch target
.maxTokens(1024)
.addUserMessage("Classify as positive/negative/neutral: " + reviewText)
.build())
.build())
// .addRequest(...) again per item — one batch can hold up to 100,000
.build();

MessageBatch batch = client.messages().batches().create(params);

// Poll until processing finishes — most batches complete within an hour
MessageBatch status = client.messages().batches().retrieve(
BatchRetrieveParams.builder().messageBatchId(batch.id()).build());
// inspect status.processingStatus() in a loop until it reports the batch has ended

// Stream results back, matched by customId
try (StreamResponse<MessageBatchIndividualResponse> results =
client.messages().batches().resultsStreaming(
BatchResultsParams.builder().messageBatchId(batch.id()).build())) {
results.stream().forEach(result -> {
// result.customId() ties this back to the request that produced it;
// result.result() carries the succeeded/errored/canceled/expired outcome
});
}






The cost tradeoff to weigh honestly: batching trades latency for a guaranteed 50% discount. It's a clear win for bulk, non-interactive work; it is the wrong tool the moment a human is waiting on the response, and it adds operational complexity (submit, poll or webhook, reconcile results by custom_id, handle errored/expired items) that a synchronous call doesn't have. Don't reach for it until you've confirmed — with real usage data — that the workload is actually bulk and actually not latency-sensitive.









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 fraction 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 the requests that actually need it.



A simple, honest pattern: run the cheap model first with instructions to flag its own uncertainty, and escalate only when it says so.




record Triage(String label, boolean confident) {}

Triage triageWithHaiku(String ticketText) {
MessageCreateParams params = MessageCreateParams.builder()
.model(Model.CLAUDE_HAIKU_4_5)
.maxTokens(100L)
.addUserMessage("""
Classify this support ticket as billing/technical/other.
If you are not confident, say so explicitly.

Ticket: %s

Respond as: <label>|<confident:yes/no>"""
.formatted(ticketText))
.build();

String reply = client.messages().create(params).content().stream()
.flatMap(block -> block.text().stream())
.findFirst()
.map(block -> block.text())
.orElse("other|no");

String[] parts = reply.split("\\|");
return new Triage(parts[0].trim(), parts.length > 1 && parts[1].trim().equalsIgnoreCase("yes"));
}

String handleTicket(String ticketText) {
Triage triage = triageWithHaiku(ticketText);
if (triage.confident()) {
return triage.label(); // cheap model handled it — done
}
// Escalate only the uncertain fraction to the stronger model
MessageCreateParams escalation = MessageCreateParams.builder()
.model(Model.CLAUDE_OPUS_4_8)
.maxTokens(500L)
.addUserMessage("Carefully classify and explain this ticket: " + ticketText)
.build();
return client.messages().create(escalation).content().stream()
.flatMap(block -> block.text().stream())
.findFirst()
.map(block -> block.text())
.orElse("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 to Opus — with no quality loss on the easy majority, because the escalation path exists precisely 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 — always check the escalation rate against a labeled sample before trusting the split, and set the threshold (here, a literal self-reported confidence flag) based on measured accuracy, not vibes.









Measure Before You Optimize



Every technique in this post has a cost of its own — a cache write premium, batch operational complexity, an extra triage call before the "real" one. None of them are free, and applied blindly they can make a system more expensive:




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

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

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



Instrument first: log cacheReadInputTokens() / cacheCreationInputTokens() / inputTokens() per request, track cost per request type, and know your actual latency requirements before reaching for any of these. 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 cacheReadInputTokens(), 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 is not a separate discipline from correctness — it's the same engineering discipline applied to a new kind of expensive, 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, and routing turns "always use the strongest model" into "use 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:

💬 Dyskusja społeczności i komentarze

🌐 Dołącz do głównego czatu

Rozpocznij dyskusję w strumieniu transmisji Social Chat lub prześlij wyżej wkład ekspercki przez formularz uzupełnień.

Community Rating &amp; Social Proof
⭐ Ocena czytelników
–
Ø / 5.0
Brak recenzji czytelników
Czy ten artykuł był pomocny?
0
Dodaj swoją ocenę
Ocena szczegółowa (opcjonalnie)
Treść i jakość
Źródło i wiarygodność
Aktualność
Security Briefing Bezpłatnie · Wypisanie 1 kliknięciem

Intelligence Digest - bezpłatnie

Codziennie najważniejsze wiadomości bezpieczeństwa · można pobrać w każdej chwili

Powiązany klaster opowieści i źródła (wektor AI)

6 Cluster
25 źródeł
CVE-2026-135550 | Mozilla Firefox Web Codecs memory corruption (Nessus ID 388625)
VulDB Updates 1 godzinę temu
24 kolejnych źródeł
VulDB Updates1 godzinę temuCVE-2026-135551 | Mozilla Firefox up to 153.7/160 Layout use after free (Nessus ID 388625) VulDB Updates1 godzinę temuCVE-2026-135552 | Mozilla Firefox up to 115.45/153.7/160 Canvas2D use after free (Nessus ID 388625) VulDB Updates1 godzinę temuCVE-2026-135554 | Mozilla Firefox up to 115.45/153.7/160 Navigation use after free (Nessus ID 388625) VulDB Updates1 godzinę temuCVE-2026-135556 | Mozilla Firefox up to 115.45/153.7/160 JIT memory corruption (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135540 | Mozilla Firefox up to 115.45/153.7/160 JIT memory corruption (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135539 | Mozilla Firefox up to 115.45/153.7/160 CSS Object Model use after free (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135542 | Mozilla Firefox up to 115.45/153.7/160 Layout buffer overflow (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135541 | Mozilla Firefox up to 115.45/153.7/160 XSLT use after free (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135544 | Mozilla Firefox up to 115.45/153.7/160 Navigation memory corruption (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135545 | Mozilla Firefox up to 153.7/160 Editor memory corruption (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135543 | Mozilla Firefox up to 115.45/153.7/160 Networking memory corruption (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135547 | Mozilla Firefox up to 115.45/153.7/160 Networking sandbox (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135549 | Mozilla Firefox up to 115.45/153.7/160 XSLT use after free (Nessus ID 388625) VulDB Updates3 godziny temuCVE-2026-135548 | Mozilla Firefox up to 115.45/153.7/160 Layout use after free (Nessus ID 388625) VulDB Updates4 godziny temuCVE-2026-135533 | Mozilla Firefox up to 115.45/153.7/160 Layout Tables off-by-one (Nessus ID 388625) VulDB Updates4 godziny temuCVE-2026-135536 | Mozilla Firefox up to 115.45/153.7/160 DOM Editor uninitialized pointer (Nessus ID 388625) VulDB Updates4 godziny temuCVE-2026-135535 | Mozilla Firefox up to 115.45/153.7/160 Skia input validation (Nessus ID 388625) VulDB Updates4 godziny temuCVE-2026-135537 | Mozilla Firefox up to 115.45/153.7/160 Layout Text/Fonts out-of-bounds (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135525 | Mozilla Firefox up to 115.45/153.7/160 WebRender use after free (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135526 | Mozilla Firefox up to 115.45/153.7/160 Networking use after free (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135527 | Mozilla Firefox up to 115.45/153.7/160 DOM/HTML use after free (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135528 | Mozilla Firefox up to 153.7/160 HTTP race condition (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135530 | Mozilla Firefox up to 115.45/153.7/160 HTTP sandbox (Nessus ID 388625) VulDB Updates5 godzin temuCVE-2026-135529 | Mozilla Firefox up to 153.7/160 Text sandbox (Nessus ID 388625)
17 źródeł
Apple Cyber Monday Deals (2026): AirPods, iPads, MacBooks, and More
WIRED 18 minut temu
16 kolejnych źródeł
The Mac Observer26 minut temuAqara W200 thermostat is $119.99 for Cyber Monday, $40 off and Apple Home ready Latest from TechRadar44 minuty temuCyber Monday mini PC deals 2026 live — top tiny desktops from GMKtec, Geekom, Minisforum, and more as the Black Friday sale continues The Mac Observer51 minut temuSonos Beam Gen 2 is $339 for Cyber Monday, $160 off and $359 under two HomePods The Mac Observer1 godzinę temuMagic Mouse in black drops to $79.99 for Cyber Monday, $19 under Apple’s $99 The Mac Observer1 godzinę temuGarmin fenix 8 47mm drops $250 to $749.99 for Cyber Monday, $49 under Ultra 4 Latest from TechRadar2 godziny temuI've been waiting for a better deal on the Philips LatteGo 4400 — and Amazon has delivered a record-low price for Cyber Monday Latest from TechRadar2 godziny temuLooking to get a last-minute gaming gift before Cyber Monday is over? WIRED3 godziny temuLast Call for Black Friday iPad Deals (2026): Save Up to $100 Latest from Windows Central3 godziny temuWhy I bought a handheld over a gaming laptop on Cyber Monday Latest from TechRadar3 godziny temuI found the most capable PC under $300 for Cyber Monday — Origimagic's C7 Windows 11 Pro machine packs dual Thunderbolt 4, 2.5Gb LAN, a Core 3 100U, 16GB of RAM, and a 512GB SSD OS X Daily3 godziny temuCyber Monday Deals: $300 Off 16″ PROLED MacBook Pro, $100 Off AirPods Pro 3, $100 Off iPad Air, & More Latest from TechRadar4 godziny temuCyber Monday home office deals 2026 live — our top workspace picks from FlexiSpot, Dell, Anker, Logitech and more as Black Friday sale rolls on Latest from TechRadar4 godziny temuThe best Cyber Monday deals in the UK in 2026: top picks from Amazon, Argos, Ninja, Lego and more Latest from TechRadar4 godziny temuBlack Friday laptop deals 2026: the huge savings on MacBooks, Chromebooks and Windows 11 laptops still available from Apple, Dell, HP and many more Latest from TechRadar4 godziny temuCyber Monday Australia laptop deals for 2026: score up to 52% off Lenovo, Apple, Dell, HP and more Latest from TechRadar4 godziny temuAmazon Australia Cyber Monday deals
12 źródeł
CVE-2026-116159 | ImageMagick up to 6.9.13-57/7.1.2-32 CDL Option memory leak (Nessus ID 388637)
VulDB Updates 1 godzinę temu
11 kolejnych źródeł
Podobne wkłady
🔍 Verwandte News

Auch interessante Nachrichten Prompt Caching and Cost Control in Java

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

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Laden...

Składki są załadowane...

Laden...

Filmy są załadowane...

Uwagi Komentarze są ładowane..
Rysunek do aktualizacji
Widżety i tematy
Ładowanie widżetów…
Następny wpis