Introduction

Great content that’s slow or expensive won’t survive production. Cost and latency aren’t afterthoughts; they’re design constraints. This article turns generative pipelines into performance systems: you’ll define budgets, pick decoder policies that finish fast, cache aggressively, batch where it pays, and measure $ per accepted output (not per token) so optimization aligns with business value.


Define Budgets First (or nothing else matters)

Set per-route caps before you tune models.

Budget doc lives next to the prompt contract and validator policy; CI blocks merges that exceed it.


Decoder Policies that Finish Fast (and good)

Decoding is your speed dial. Defaults that work for most short/medium forms:

Rule: tune for first-pass pass-rate (CPR) × tokens generated. A policy that reduces resamples often beats micro token savings.


Sectioned Generation = Deterministic Latency

Generate by section with stop sequences and per-section max_tokens. Benefits:

Example caps: Overview ≤ 120 tokens, Benefits (3 bullets × ≤ 18 words), CTA ≤ 25 tokens.


Caching That Actually Moves the Needle

Think three layers:

  1. Template/Frame Cache: pure config (templates, style frames, validator policies). 100% hit-rate, zero risk.

  2. Retrieval/Claim Cache: shaped claim packs for common topics/segments, keyed by topic+region+freshness_window. Invalidate on source change.

  3. Generation Cache: deterministic sections (low temp, no randomness) for repeated assets like disclosures, footers, or boilerplate intros.

Track cache hit-rate × tokens saved. If a cache doesn’t lift $/accepted measurably, delete it.


Batching & KV-Cache Reuse

Where the runtime supports it:

Guardrail: cap parallelism to avoid contention spikes; prioritize p95 over hero p50.


The Repair Loop is a Cost Center—Trim It

Repairs are necessary but expensive. Reduce them by:

Track repairs per accepted; set a hard budget (e.g., ≤ 0.25 sections repaired per output).


Routing & Model Mix

Use the smallest model that hits CPR + latency SLOs; escalate on uncertainty or risk.

Metric: escalation rate vs. win-rate. If escalations don’t materially improve acceptance or downstream KPI, tighten thresholds.


Measure What Pays: $/Accepted Output

Forget raw token bills. Compute:

[
\text{$/accepted}=\frac{\text{LLM $}+\text{retrieval $}+\text{repair $}+\text{selector $}}{\text{accepted outputs}}
]

Where:

Target down-and-right over releases without hurting CPR.


Observability: What to Put on the Dashboard

Per route, by model & release:

Add alerts: p95 latency +20%, CPR −2 pts, $/accepted +25% → auto-canary pause.


Optimization Playbook (in priority order)

  1. Cut wasteful tokens: shorten headers, remove vestigial instructions, compress examples.

  2. Section caps + stops: reduce overrun and long tails.

  3. Decoder tuning: lower top_p/temperature on sections that fail validators.

  4. Cache claim packs for hot topics; ensure freshness policy.

  5. Speculative decoding for long sections; evaluate real-world accept rate.

  6. Route more to SLM as CPR stabilizes; raise escalation threshold carefully.

  7. Trim variants: if selector ROI flattens, reduce N.


Worked Example (Composite)

Baseline (Part 4 pipeline, blog route):

Changes

Result (2-week canary)


Anti-Patterns (and fixes)


Minimal Config (copy/paste)

Decoder policy

{"top_p":0.9,"temperature":0.7,"repetition_penalty":1.05,
 "section_max_tokens":{"overview":120,"benefits":140,"proof":220,"cta":30}}

Budgets

{"header_tokens_max":200,"context_tokens_max":800,"gen_tokens_max":220,
 "p95_latency_ms_max":1200,"target_cpr":0.92,"repairs_per_accepted_max":0.25}

Canary auto-halt

{"cpr_drop_pts":2.0,"p95_increase_pct":20,"cost_increase_pct":25}

Conclusion

Cost and latency engineering is mostly discipline: budgets at the top, decoder policies that reduce retries, sectioned generation with hard stops, caches that save real tokens, and routing that keeps the big guns for the rare case. Track CPR, time-to-valid, tokens/accepted, repairs/accepted, and $/accepted—and wire auto-halts so mistakes are cheap. With these habits, your generative stack gets faster and cheaper every release without sacrificing quality.