Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 63. Reliability
  4. 63.4

Lesson 63.4 · 4 of 4 · 4 min read · Reviewed 24 Nov 2026

Observability and logging

By the end, you can

  • Log what you need to debug agent runs.
  • Track a few key metrics for reliability.

When an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. goes wrong, you need to see what it saw and what it did.

Log per run

  • A run id that links everything.
  • Each model call: prompt version, model, input and output tokensOutput tokens: The tokens the model generates in its reply, including any tool calls and thinking., latency, stop reasonStop reason: The field in a response that says why generation stopped: end_turn, max_tokens, stop_sequence, tool_use and others..
  • Each tool call: name, input, result (trimmed), error flag, duration.
  • Decisions: escalationsEscalation: Handing a case from an agent to a person, with the context needed to continue., fallbacks, retries.

Watch a few metrics

Metric Why
Task success rate The headline
Error and retry rates Spot outages and bad tools
Latency (p50, p95) User experience
Cost and turns per task Catch runaway loops
Escalation rate Too high wastes people; too low may hide problems

Privacy

RedactRedaction: Removing or masking sensitive data, such as PII or secrets, before it is stored, logged or sent. secrets and personal dataPII: Personally identifiable information: data that can identify a person, such as name, email, address or ID numbers. before logs leave the process, and set retention limits.

Chapter 67 builds this into full tracingTrace: A record of everything that happened during one request or agent run, as a tree of timed steps., with dashboards and alerts.

Key points

  • Log every model call and tool call with inputs, outputs, tokens, latency and errors, linked by a run id.
  • Watch error rates, latency, cost per task, turns per task and escalation rate.
  • Redact secrets and personal data before logging.
Hands-on labRuns in the Playground

Log an agent run with redaction

Write structured, redacted logs for one agent run and compute its key metrics.

You will have: A log() helper that adds a run id and redacts emails, plus a printed run summary.

Steps

  1. Run the starter and spot the raw email address in the tool call log.
  2. Add the run id to every record so all events of a run link together.
  3. Redact emails in every string field before the record is stored.
  4. Print tool calls, tool errors and total tokens for the run.

Done when

  • Every printed record has "run_id": "run-0007".
  • The summary shows 3 tool calls, 1 error and 3520 total tokens.
  • No email address is left in the logs.

Open in the Playground No API key needed.

Starter code (Python)
import json, re
# Structured logs for one agent run, with redaction before anything is written.
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")
LOGS = []

def log(event, **fields):
    # TODO: add the run id to every record and redact emails in every string field
    LOGS.append({"event": event, **fields})

RUN_ID = "run-0007"
log("model_call", prompt_version="v3", input_tokens=1200, output_tokens=180, latency_ms=900, stop_reason="tool_use")
log("tool_call", name="find_customer", input="jane@example.com", ok=True, ms=120)
log("tool_call", name="get_orders", input="cust-55", ok=False, ms=3000, error="timeout")
log("tool_call", name="get_orders", input="cust-55", ok=True, ms=200)
log("model_call", prompt_version="v3", input_tokens=1900, output_tokens=240, latency_ms=1100, stop_reason="end_turn")

for r in LOGS:
    print(json.dumps(r))
# TODO: print tool_calls, tool_errors and total tokens for the run
Show a solution
import json, re
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.]+")
LOGS = []
RUN_ID = "run-0007"

def redact(v):
    return EMAIL.sub("<EMAIL>", v) if isinstance(v, str) else v

def log(event, **fields):
    LOGS.append({"run_id": RUN_ID, "event": event, **{k: redact(v) for k, v in fields.items()}})

log("model_call", prompt_version="v3", input_tokens=1200, output_tokens=180, latency_ms=900, stop_reason="tool_use")
log("tool_call", name="find_customer", input="jane@example.com", ok=True, ms=120)
log("tool_call", name="get_orders", input="cust-55", ok=False, ms=3000, error="timeout")
log("tool_call", name="get_orders", input="cust-55", ok=True, ms=200)
log("model_call", prompt_version="v3", input_tokens=1900, output_tokens=240, latency_ms=1100, stop_reason="end_turn")

for r in LOGS:
    print(json.dumps(r))
tools = [r for r in LOGS if r["event"] == "tool_call"]
models = [r for r in LOGS if r["event"] == "model_call"]
print("tool_calls:", len(tools), "| tool_errors:", sum(not r["ok"] for r in tools))
print("total tokens:", sum(r["input_tokens"] + r["output_tokens"] for r in models))
print("emails left in logs:", sum(bool(EMAIL.search(json.dumps(r))) for r in LOGS))
Quick checkA user says the agent gave a wrong refund amount yesterday. What makes this quick to investigate?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.