AI Model Serving and Production LLM System Design
Design model gateways, token budgets, inference queues, streaming, evaluation, security, and AI-specific observability.
LLM Gateways, Routing, and Token Budgets
An LLM gateway can centralize provider credentials, model routing, quota enforcement, request validation, and telemetry. Token budgets include instructions, user input, retrieved context, tool output, and expected generation. A request that fits one model may exceed another model's context window.
Model routing can optimize cost, latency, capability, or availability, but fallback models may differ in quality and output format. Validate structured outputs and define timeouts, retries, and safe fallbacks. A provider may have completed a request even if the client lost the response, so repeated expensive calls need careful handling.
Apply per-tenant token and concurrency quotas. Track cost per successful task rather than only request count because prompt and output lengths vary substantially.
- Budget for all prompt and output tokens.
- Model fallback requires output and quality validation.
- Token quotas are more informative than request counts alone.
Inference Batching, Streaming, and GPU Capacity
Inference capacity depends on model size, sequence length, concurrency, batching, and accelerator memory. Processing input tokens and generating output tokens have different performance characteristics. Batching improves accelerator utilization but can increase queue delay; continuous batching can improve utilization while admitting and retiring sequences dynamically.
Streaming partial output improves perceived latency but introduces cancellation, partial-response, and billing semantics. Decide whether generation should stop when the client disconnects and how incomplete outputs are represented. Autoscaling model servers may be slower and more expensive than scaling stateless APIs, so admission control and capacity headroom are important.
Track time to first token, tokens per second, queue delay, errors, GPU memory, utilization, and cost per completed request. Avoid accepting unlimited queued prompts during provider or GPU overload.
- Batching trades utilization against queueing latency.
- Streaming requires cancellation and partial-result semantics.
- Track inference-specific metrics, not only HTTP latency.
AI Evaluation, Prompt Injection, and Observability
Probabilistic applications need evaluation datasets that cover representative tasks, adversarial inputs, and known failure cases. Measure task success, groundedness, retrieval quality, refusal correctness, latency, and cost. Human review remains useful when automatic scores do not reliably reflect user value.
Treat retrieved documents and tool results as untrusted input. Prompt injection may try to override instructions or induce unauthorized actions. Enforce tool permissions and resource authorization in application code, validate arguments against narrow schemas, and require confirmation for high-impact operations.
Record model version, prompt version, retrieval version, and trace identifiers while minimizing sensitive prompt retention. Redact secrets and personal data. Canary model or prompt changes, compare evaluation results, and keep rollback paths for quality or safety regressions.
- Evaluate AI against task-specific test sets.
- Do not rely on model instructions as the sole authorization boundary.
- Version prompts, models, and retrieval indexes as dependencies.