I learned the word hallucination the fun way: watching a model confidently invent a fact that never existed. My first thought was not deep or thoughtful. It was: "what the crap?" The second thought was, "okay, show me exactly how you got there." That's where the real problem showed up.
No logs. No trace. Just vibes.
If you're running AI in production and your logging story is "we log errors," you're toast. AI incidents are almost never a nice clean exception with a stack trace. They're "the agent did something weird," and now you have to reconstruct a conversation, a tool call, a model version, and a bunch of hidden context that quietly changed underneath you.
First rule: log the workflow, not just the API call
An AI request is a pipeline. Even a “simple” chat turns into a chain:
- user message
- system prompt + hidden instructions
- retrieved context (RAG)
- tool calls (search, DB, email, ticketing, whatever)
- model output
- post-processing (parsers, validators, safety filters)
If you can't replay that chain, you can't debug it.
The minimum I want for each step:
- Prompt + template version (with redaction)
- Model name + provider + version (and region if it matters)
- Inputs to tools and tool outputs (redacted)
- Final output (and intermediate outputs for multi-step agents)
- Latency per step, not just total time
- Token counts (prompt, completion, total)
- Refusal / safety flags from your guardrails
- Correlation IDs across services
If you have tenants, log tenant. If you have environments, log environment. If you have users, log a user identity or a stable hash. Otherwise you'll stare at a spike on a dashboard thinking, "Cool. Which customer is angry?"
Correlation IDs: the boring glue that saves your weekend
Give every “AI run” a run_id. Every tool call gets a tool_call_id. Every downstream service call carries those IDs. It's boring plumbing, and it's the difference between knowing and guessing.
A simple structured log shape:
{
"ts": "2026-04-14T18:22:11Z",
"env": "prod",
"tenant_id": "t_123",
"user_id": "u_456",
"run_id": "run_9c1f",
"step": "tool_call",
"tool_name": "crm.search",
"model": "gpt-4.1-mini",
"prompt_version": "agent_v17",
"latency_ms": 842,
"tokens_in": 3120,
"tokens_out": 220,
"redaction": "pii_v3",
"payload": {
"query": "[REDACTED_EMAIL]"
}
}
You don't have to start fancier than this. Just make sure everything passes the same IDs around.
Redaction and minimization: logs are a second data leak waiting to happen
I worked at a SaaS shop that advertised “AI automation,” and then I found out a big chunk of it was spreadsheets, formatting, and humans doing the work on the side. That mess made me paranoid in a useful way: if your process is sloppy, your data boundaries are sloppy too.
Logging can quietly turn into a shadow database of secrets.
Rules that actually hold up:
- Default to structured logs so you can mask by key instead of playing regex roulette.
- Store raw prompts separately (if you really need them) behind tighter access controls. Most people only need hashes and metadata.
- Redact at the edge before the log event is emitted. "We'll sanitize later" is how you end up with secrets in a BI tool.
- Minimize: you don't need full tool outputs most of the time. Log a summary and a pointer.
- Retention: keep sensitive traces for as little time as you can get away with. If you keep it forever, you own it forever.
And yes, you need to test your redaction. Not by reading the code and nodding. By actively trying to punch holes in it.
Monitoring the AI stack: watch behavior, not just uptime
Traditional monitoring asks: "Is the service up?" AI monitoring asks: "Is the service behaving like itself?"
Metrics I like:
- Tool call distribution (counts by tool, per feature, per tenant)
- Refusal rate and guardrail trigger rate
- Parse/validation failures (JSON schema errors, function argument errors)
- Anomaly spikes: token usage, latency, tool retries
- RAG quality proxies: retrieval hit rate, empty-context rate
Dashboards should slice by:
- feature (chat, summarization, extraction, agent)
- tenant (because one customer can be the whole fire)
- model version (because “upgrades” have teeth)
Alerts that matter: "weird but safe" vs "call someone now"
Most alert setups are noise because they’re built for exceptions, not for “the AI lost the plot.”
Signals for possible abuse:
- repeated jailbreak attempts (same user, same tenant, tight time windows)
- unusual tool usage (sudden spike in
export,download,admin.*) - mass queries (high volume + high token usage + broad filters)
- prompt injection patterns in retrieved documents (you should log retrieval sources)
Signals for “wake a human up”:
- model output contains sensitive fields that should never leave a vault
- tool calls hitting privileged endpoints unexpectedly
- guardrails disabled or bypassed
- a new prompt/model version causing a sharp behavior change
My favorite filter is simple: alert on deltas. If a tenant's tool usage jumps 10x in 10 minutes, that's not a fun traffic pattern. That's “go look right now.”
Incident response for AI: isolate, disable, rollback
AI incidents need playbooks that assume the model is a chaotic intern with root access.
Triage steps that tend to work:
- Freeze the evidence: capture the
run_idtraces and related tool logs. - Isolate the blast radius: disable or throttle the risky tool, not the whole product.
- Rollback prompt/model versions: treat prompts like deployable artifacts with versions and changelogs.
- Add a temporary guardrail: block a tool call pattern, enforce allow-lists, clamp arguments.
- Reproduce with a replay harness: same prompt, same retrieved docs, same tool stubs.
Playbooks I keep nearby:
- prompt injection via RAG (e.g., "ignore previous instructions" hiding in docs)
- data leak from over-sharing tool output
- over-permissioned tool misuse (agent has "search" but also "export all" for some reason)
The principle of least privilege in the AI age means scoping every tool to the minimum permissions you can live with—because hallucinations turn from goofy chat errors into expensive, real-world damage the moment agents get broad access.
Post-mortems that actually harden the system
A post-mortem only matters if it changes code and tests, not just slides.
Feed the lessons back into:
- prompt templates (versioned and reviewed like normal code)
- tool permissions (scoped tokens, allow-lists, better defaults)
- evaluation suites ("incident scenarios" you can replay in CI)
Treat incidents like new unit tests. If you can't simulate the failure on demand, you didn't really fix it.
Production incidents don't care about your diagrams. They care about whether you can reconstruct what happened, quickly, without guessing. Logging the whole workflow is how you stop guessing.
-Sethers