Production AI Engineering
Observability, reliability, cost, latency, API design, CI/CD for AI and the readiness checklist.
A demo only proves the best case. For real use, record every step, handle failures sensibly, set cost limits and plan for speed. Treat prompts and settings as code: version them, test them and release them gradually. Use a checklist before launch.
- Correlation ID
- A label that follows one request through the whole system.
- p95 latency
- The time within which 95 of 100 requests finish.
- Canary release
- Rolling out to a small group first.
A demo only proves the best case.
A demo proves that the happy path works. Production engineering proves that the unsafe, expensive, unavailable, incorrect and hostile paths are under control. This lesson covers observability, reliability, cost, speed, API design, managing settings and the production-readiness checklist.
Observability and tracing
Ordinary logs are not enough. You need traces that show model calls, prompts or safe prompt details, search results, tool calls, delay, tokens, errors, retries and final outcomes. Use correlation IDs all the way along the chain: user request, agent run, model call, search, tool call and outside service. Store enough to debug without logging sensitive data you do not need. Dashboards should show task success, delay percentiles, model error rate, tool error rate, token use, cost and escalation. For agents they should also show steps per task and the reasons runs ended.
- Trace the whole execution graph of the agent.
- Remove secrets and sensitive content from telemetry.
- Make cost and reliability visible for each workflow.
Reliability, retries and recovery
AI systems fail in particular ways. The provider times out. Rate limits hit. Outputs are malformed. Tools fail. Search fails. The context overflows. Model quality gets worse. Retry selectively. Retry short-lived infrastructure errors with longer waits and jitter. Do not blindly retry invalid tool inputs forever. Use circuit breakers and fallbacks for dependencies that are struggling. Long-running agents need checkpoints and the ability to resume. If a process fails after five good steps, resume from a saved checkpoint. Do not start from zero.
TRANSIENT timeout, 429, 503 -> retry with backoff + jitter (bounded), then fall back or fail clearly PERMANENT 400, auth failure, bad schema -> do NOT retry; surface to caller / fix the prompt or schema POLICY denied by authorization -> never retry; explain, or escalate to a human QUALITY valid but wrong output -> repair once with the validator's errors, then escalate; fix the root cause
Cost and token economics
AI cost comes from model prices, input and output tokens, search, running tools, infrastructure and retries. Agent loops multiply cost, because one user request may trigger many model calls. Measure the cost for each successful business task. A cheap model that often fails can cost more than a stronger one that succeeds the first time. Common savings are caching, shorter context, better search, smaller models for simple tasks, batching, parallel work where it suits, and limiting agent rounds. Cost controls belong in the architecture. They do not belong in a finance report after launch.
- Set budgets for each request and each workflow.
- Track tokens by customer and by feature.
- Optimize the cost of a successful task, not token count alone.
Delay and performance
Delay has many parts: network, model queueing, inference, search, tool calls, serialization and orchestration. Streaming improves how fast it feels, but it does not reduce the total work. Running search and tool calls in parallel cuts waiting time when the operations are independent. Caching removes repeated work. Set a delay budget for each workflow. A real-time chat assistant needs a different design from a background report agent. Measure p50, p95 and p99, and move long-running work to background jobs.
API design, Git and CI/CD for AI
AI APIs should offer clear operations, not one endpoint that accepts any instruction. Useful fields include a request ID, customer ID, user context, task type, input, settings and response details. Long tasks return a run ID and a status endpoint. Version your contracts. And remember that a change of model or prompt can change behavior even when the API schema stays the same. So record settings and version details internally, for tracing.
Prompts, tool schemas, evaluation datasets, model settings and policies are code. They belong in version control. CI should run fixed tests, schema checks, security checks and chosen AI evaluations. A model upgrade should not ship just because the application builds. Use staged rollouts and canaries. Compare quality, cost, delay and failure rates before the full rollout.
Production readiness checklist
- Identity, authorization and customer isolation are checked.
- Defenses against prompt injection and limits on tools are in place. Schemas are checked.
- Rate limits, budgets, timeouts, retries and cancellation are built.
- Audit logs, tracing and dashboards with alerts exist.
- An evaluation baseline and regression tests exist. Model and version tracking is in place.
- Incident response, data retention and a tested rollback plan are written down.
- ✓Trace the full agent execution graph with correlation IDs. Remove sensitive content.
- ✓Sort errors as short-lived, permanent, policy or quality. Retry selectively with longer waits and jitter. Checkpoint long work.
- ✓Optimize cost per successful task, with budgets and tracking for each customer.
- ✓Treat prompts, schemas, datasets and model settings as code. Version them, gate them with evals, and use canaries.
Make the minimal LLM service production-ready
- Add tracing with a correlation ID from the request through the model and tool calls. Remove at least emails and API keys.
- Add error classification and limited retries. Test with a mocked storm of 429 errors and a malformed-output case.
- Add a token budget for each request and each customer, that stops an endless loop you cause on purpose.
- Write the readiness checklist for your service, with a link as evidence for every item. Mark what fails.
Q1Why must you not blindly retry invalid tool inputs?show answer
The failure is permanent. It is a problem in the prompt or schema. Retries burn tokens and time, and hide the root cause. Repair once with feedback from the checker, then escalate.
Q2Does streaming reduce total delay?show answer
It improves how fast it feels, but not the total work. Real reductions come from parallel work, caching, smaller models and asynchronous design.
Q3Why gate model upgrades with evals?show answer
Behavior can change with the model even if the code builds. Evals and canaries compare quality, cost, delay and failure rate before the full rollout.