The first time we used an AI assistant during an incident, an engineer pasted a raw pod log, a kubectl describe output and the full environment of the failing container into the chat. The assistant found the bug in a minute. It also now had a database password, two API keys and a handful of customer email addresses. Nobody had planned it; it was just what was on the screen at 2 a.m.

Since then we treat "context for the AI" as a defined artifact with a checklist, like a postmortem template. This post describes the bundle we assemble, how we redact it, how we give agents access without giving them the ability to change production, and how we check the answer before acting on it.

What the assistant actually needs

Root cause analysis needs the same evidence a senior engineer would ask for. More is not better: unrelated lines invite the model to build a story around them. Our bundle has six parts:

Part

Content

Size target

Summary

Symptom, start time, impact, what is already ruled out

5-10 lines

Metrics

Error ratio, latency, saturation numbers around the start time

a few values, not screenshots

Logs

Deduplicated error lines plus all lines of one failing trace

under 200 lines

Trace

One failing trace as span list: name, duration, status, key attributes

20-60 spans

Change

Diff between the last good and first bad version, config and flag changes

the diff, not the repo

Environment

Runtime, library and infrastructure versions

a table

The "already ruled out" line is easy to forget and saves the most time. Without it, the assistant suggests the three things you checked first.

Building the bundle

We have a small script that the on-call engineer runs with the trace ID and the two versions. Each step is ordinary tooling:

TRACE=4bf92f3577b34da6a3ce929d0e0e4736
GOOD=v1.42.0; BAD=v1.43.0
OUT=incident-bundle; mkdir -p "$OUT"

# 1. logs for one trace, ordered, only useful fields
logcli query --since=2h --limit=5000 --output=jsonl \
  "{env=\"prod\"} | json | trace_id=\"$TRACE\"" \
  | jq -c '{ts: .timestamp, line: .line}' > "$OUT/trace-logs.jsonl"

# 2. distinct error events in the window, with counts
logcli query --since=1h --limit=20000 --output=raw '{service="orders-api"} | json | level="error"' \
  | jq -r '.event + " " + (.error_type // "-")' | sort | uniq -c | sort -rn > "$OUT/error-counts.txt"

# 3. what changed in code and config
git log --oneline "$GOOD..$BAD" > "$OUT/commits.txt"
git diff --stat "$GOOD..$BAD" > "$OUT/diffstat.txt"
git diff "$GOOD..$BAD" -- src/ config/ > "$OUT/code.diff"

# 4. runtime and versions, never env values
kubectl -n orders get deploy orders-api -o jsonpath='{.spec.template.spec.containers[*].image}' > "$OUT/images.txt"

Note step 4. We record image tags, not kubectl describe pod output, because describe prints environment variables that came from literal values in the manifest. If you need environment information, list variable names only.

Redaction before anything leaves our network

The bundle goes through a redaction pass before it is pasted, uploaded or read by an agent. Logger-level redaction should already have removed most secrets, but incident bundles also contain diffs, config and command output that never went through a logger. The redactor is deliberately simple and conservative:

import re, sys
from pathlib import Path

PATTERNS = [
    (re.compile(r"(?i)(authorization:\s*bearer\s+)[A-Za-z0-9._~+/=-]+"), r"\1[REDACTED]"),
    (re.compile(r"eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}"), "[JWT]"),
    (re.compile(r"(?i)\b(password|passwd|secret|api[_-]?key|token)\b(\s*[=:]\s*)\S+"), r"\1\2[REDACTED]"),
    (re.compile(r"(?i)\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b"), "[EMAIL]"),
    (re.compile(r"\b(?:\d[ -]?){13,19}\b"), "[CARD?]"),
    (re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"), "[IP]"),
    (re.compile(r"(postgres(?:ql)?|mysql|redis)://[^@\s]+@"), r"\1://[CREDS]@"),
]

def redact(text: str) -> str:
    for pattern, repl in PATTERNS:
        text = pattern.sub(repl, text)
    return text

for path in Path(sys.argv[1]).rglob("*"):
    if path.is_file():
        path.write_text(redact(path.read_text(errors="replace")))

It over-redacts on purpose. A long order number that looks like a card is replaced, which costs nothing for debugging. We also run a secret scanner such as gitleaks over the folder (gitleaks dir incident-bundle) as a second check, because regexes miss provider-specific key formats.

Two rules sit above the tooling:

  • Use an approved AI service. Incident data goes only to an assistant covered by our organisation's data processing agreement and retention settings, never to a personal account.

  • Raw customer content stays out. If the bug depends on the content of a specific record, describe its shape ("a name field with a 300-character value containing a newline") instead of pasting it.

Read-only access for agents

When an agent such as Claude Code investigates directly instead of reading a bundle, the risk shifts from leaking data to changing things. We give it the same access we would give a new contractor on day one: read, never write.

  • Kubernetes: a dedicated kubeconfig bound to the built-in view ClusterRole, which excludes Secrets. No exec, no port-forward.

  • Database: a read-only role on a replica, with a statement timeout so a heavy query cannot hurt production.

  • Cloud: a role with read-only permissions, without access to secret stores or KMS decrypt.

  • Agent permissions: project settings deny mutating commands outright, so even a mistaken suggestion is blocked before it runs.

{
  "permissions": {
    "allow": ["Bash(kubectl get:*)", "Bash(kubectl logs:*)", "Bash(git log:*)", "Bash(git diff:*)"],
    "deny": [
      "Bash(kubectl delete:*)", "Bash(kubectl apply:*)", "Bash(kubectl exec:*)",
      "Bash(kubectl edit:*)", "Bash(kubectl scale:*)", "Read(./**/.env)", "Read(./**/secrets/**)"
    ]
  }
}

Deny rules are a guardrail, not the security boundary. The boundary is the credential: a view kubeconfig cannot delete a pod no matter what the agent types.

Verifying the hypothesis

Models produce fluent, confident explanations, including wrong ones. We ask for the answer in a form that can be checked:

Given the bundle, give:
1. The single most likely root cause, in one sentence.
2. Two observations in the bundle that support it, quoted with file and line.
3. One prediction that must be true if you are right and that we can check
   with a query or command not already in the bundle.
4. The next most likely cause, and what would distinguish it from the first.

Point 3 is the important one. "If the new retry wrapper is the cause, then requests on v1.42.0 pods in the same window should show zero payment.capture.retried events" is a query we can run in seconds. If the prediction fails, the hypothesis is wrong, however convincing it reads. Point 2 catches invented evidence: if the quoted line is not in the bundle, we stop trusting the rest of the answer.

Then the normal rules apply: reproduce in staging when possible, make the smallest fix, and confirm with the same metric that showed the incident.

Common mistakes

Mistake

Why it hurts

Do this instead

Pasting kubectl describe or env dumps

Literal secrets in the chat

Image tags and variable names only

Whole log files

Model latches onto unrelated errors

One trace plus deduplicated error counts

Agent with the on-call engineer's credentials

One bad command away from an outage

Dedicated read-only identity

Acting on the first answer

Fluent does not mean correct

Require quoted evidence and a checkable prediction

Personal AI accounts

Data leaves under unknown terms

Approved service with agreed retention

Key takeaways

  • Treat AI incident context as a defined bundle: summary, metrics, one trace, logs, diff, versions.

  • Redact everything that did not pass through a logger, and run a secret scanner as well.

  • Give agents read-only identities; permission rules in the agent are a second layer.

  • Ask for quoted evidence and a falsifiable prediction, then check it before acting.

References