01Separate planes and stages
The edge authenticates and reserves quota; orchestration assembles context and policy; the model gateway routes; inference workers batch and stream; tool runners execute behind isolation; telemetry and eval pipelines learn from outcomes.
Trace an enterprise assistant in stages. The application authenticates the caller, assembles authorized evidence, asks the model for text or a proposed tool call, validates any action, and publishes the result. The model is one stage in a larger system. Distinguish a bad answer from a failed request, an unauthorized retrieval, and a duplicated action; each needs different telemetry and recovery.
Keep policy and durable run state outside the generated conversation. Record tenant, input identity, selected versions, tool attempts, and terminal status in application-owned storage. A control plane changes model routing, quotas, or tool policy; the data plane serves runs under a known policy version. A control-plane outage should have an explicit effect on existing and new runs.
Streaming improves perceived responsiveness but exposes partial state. Label output that is incomplete, record a terminal result, and let clients inspect a run after disconnection. Do not assume that displaying a plausible final sentence means an external action succeeded. Persist effects and authoritative tool receipts separately from generated prose.
- Track time-to-first-token separately from total time
- Cancel work when the client disconnects
- Keep authorization attached to retrieved context and tools
02Optimize tokens as the core resource
Input tokens consume prefill compute and KV memory; output tokens consume sequential decode time. Prefix caching, continuous batching, smaller context, and model routing change both latency and cost.
Model request cost depends on work shape: input length, generated length, model choice, retries, tool loops, and reused prefixes. Two requests can differ by orders of magnitude in work while counting equally in RPS. Reserve estimated tokens and spend at admission, bound output and step counts, then reconcile actual usage including failed attempts.
Separate queue delay, prefill latency, and decode speed. A faster first token does not guarantee faster completion if generation is long. A cache hit on a reusable prefix can help prefill without making a stale retrieved document safe. Cache identity must include the inputs and versions that determine behavior; permission checks remain a separate application responsibility.
When capacity is scarce, choose degradation by user need. A long research task may become an asynchronous job, while a short interactive extraction may be prioritized. A smaller model is a valid fallback only if it satisfies the required capability and quality bar. Make these choices visible in the product contract and evaluation slices.
- Batch compatible work without excessive waiting
- Budget context among instructions, history, retrieval, and tool results
- Cache only when privacy and semantic equivalence are clear
03Bound probabilistic behavior
Wrap model decisions in deterministic checks: schemas, policies, permission gates, time/cost budgets, loop detection, and idempotent tools. Evals test end-to-end outcomes, not only prose quality.
An LLM can propose a tool argument; deterministic code must authorize the principal, resource, and effect. Bind approval to the exact action parameters and recheck relevant permissions before execution. If access changes while a run waits, the old conversation is not a fresh grant. The same rule applies before publishing restricted evidence to the user.
Define completion with evidence: cited passages for a knowledge answer, a durable operation receipt for an action, or an inspected artifact for a report. Then build an evaluation plan with unsupported answers, stale sources, denied permissions, duplicate calls, and provider failure. Test the whole path because a strong model cannot compensate for a broken authorization filter.
Before drawing a complicated agent architecture, compare a fixed workflow. If steps are known—retrieve policy, check account, propose reply—a workflow offers explicit state and recovery. Use a bounded adaptive loop only where observations genuinely change the path. Its budgets, stop conditions, and escalation behavior belong in the design from the start.
- Fallback quality change must be product-visible when material
- Moderation has input/output/tool surfaces
- Telemetry needs model and prompt versions