Topic 5 of 5 · 4 min read
Instrumentation gaps in agent-written code
Goal: Find missing or harmful instrumentation in an agent's diff and fix it so failures become visible.
Review an agent's diff for what it hides as much as for what it does: swallowed errors, silent fallbacks, missing context and secrets in logs. Agents such as Claude Code, Codex, Copilot and Cursor are asked to make tests pass and avoid crashes, and an error that is swallowed makes both true.
Check yourself · 1 of 3
An agent wraps a model call in try/catch and returns an empty string on error. What is the main observability problem?
Why it matters
A feature that fails loudly gets fixed in an hour. One that fails silently can run wrong for weeks. Agent-written code often adds a try/catch that returns a default, a retry loop with no log, or a debug print of the whole object. All of them look tidy in the diff, and all of them make production harder to understand.
What to look for
- Swallowed errors.
catch {}orexcept: pass, or a catch that returns an empty value. The failure should at least be logged with context and counted. - Silent fallbacks. "If the model call fails, use the cached answer" is fine only if you can see how often it happens. Add a log or a counter.
- Missing context. New log lines without request id, order id or other identifiers; new background jobs and queue messages without trace context.
- Secrets and personal data. Logging
req,headers,env, prompts, tokens or whole user records. - New I/O with no signal. A new HTTP call, database query or model call with no span, duration or error count.
- Wrong level. Expected conditions at
error, or real failures atdebug. - High-cardinality labels on new metrics.
Before and after
async function summarise(doc: Doc) {
- const res = await llm.complete(buildPrompt(doc));
- return res.text;
+ try {
+ const res = await llm.complete(buildPrompt(doc));
+ console.log("llm response", res, process.env.OPENAI_API_KEY);
+ return res.text;
+ } catch (e) {
+ return "";
+ }
}
This hides every failure behind an empty summary and prints an API key. The fixed version logs the event with the document id and error code, increments a failure counter, records token counts on a span, and rethrows or returns a typed error the caller can handle. It never prints the key.
What goes wrong
- Approving because the tests pass. The test passes since the error is swallowed.
- Only reading the changed lines. The missing signal is often what is not in the diff.
- Fixing by adding more logs. Ten unstructured lines are worse than one good event.
Examples
The silent retry
for _ in range(5):
try:
return client.post(url, json=payload)
except Exception:
time.sleep(1)
Five failures, no log, and the loop falls through to None. Review comment: log each attempt with the attempt number and error, raise after the last one, and count retries.
A log line with a secret
An agent adds logger.info("calling provider", extra={"headers": headers}) while debugging. The headers contain Authorization: Bearer .... Ask for the line to log the provider name and request id only, and check old log lines for the same leak.
Key points
- Look in the diff for catch blocks that return defaults, because they turn failures into silent wrong answers.
- A fallback is acceptable only if you can see how often it fires, through a log or a counter.
- New log lines need request or trace context and must not include tokens, headers, prompts or whole user records.
- New I/O such as an HTTP call or a model call needs a span, a duration and an error count.
- A passing test suite does not prove failures are visible; ask what an operator would see when this breaks.
Common mistakes
- Accepting a try/catch that returns an empty value because it makes the test green.
- Reviewing only for correct behaviour and never asking what would be visible on failure.
- Letting debug prints of whole objects reach main.
- Adding a log line to every function instead of one structured event at the failure point.
Check yourself · 2 of 3
Which new log line in a diff should you reject?
Check yourself · 3 of 3
A silent fallback returns cached data when the live API fails. What makes it acceptable?
Practice items, not counted towards your mastery.
Start here
- OpenTelemetry documentation (opens in a new tab)DocsFree
The vendor-neutral standard for traces, metrics and logs, with concepts and language guides.
- Monitoring Distributed Systems (Google SRE Book) (opens in a new tab)BookFree
Defines the four golden signals and explains why to page on symptoms and use percentiles.
- Alerting on SLOs (Google SRE Workbook) (opens in a new tab)BookFree
Worked burn-rate alerts that turn an SLO into pages that matter.
More for this topic
- Logging Cheat Sheet (OWASP) (opens in a new tab)ArticleFree
What to log, what never to log, and how to keep logs usable and safe.