Skip to content
mentoor

Topic 1 of 4 · 5 min read

Security logging for agents and tool calls

Goal: Specify the fields an agent audit log needs to reconstruct what it did and why, without storing secrets.

An agent's security log must let you replay a run after the fact: who started it, what it read, which tools it called with which arguments, what a human approved, and what came back, with secrets kept out. If you cannot answer "what did the agent do, and why?" from the log alone, you cannot investigate an incident.

Check yourself · 1 of 3

An agent run leaked data after reading a web page. Which log field most helps you find where the malicious instruction came from?

Choose one answer

Why it matters

A normal web request has one input and one output. An agent run is a chain: a prompt, several model calls, tool calls, documents it fetched, and decisions in between. Prompt injection often hides in one fetched page three steps back. Without a record of what was read, you will see the damaging tool call but never learn where the instruction came from. Model output is also not repeatable: running the same prompt again may give a different answer, so you cannot "just reproduce it". The log is your only evidence.

What to record

Give every run a run_id and every step a step_id and parent_step_id, so the chain can be rebuilt. Then log these:

  1. Identity. The user, the tenant, the agent and its version, and the credentials it ran as (an id, never the token).
  2. Inputs. The system prompt version (a hash or version id), the user message, and every external source read: URL, file path or document id, plus a hash of the content.
  3. Model calls. Model name, token counts, and the stop reason. Store full prompts and replies only where your policy allows, because they often contain personal data.
  4. Tool calls. Tool name, arguments, result status, result size, duration, and the target of any side effect (the email recipient, the file path, the host).
  5. Approvals. Who approved or denied an action, when, and what exactly they saw.
  6. Policy decisions. Any guardrail that blocked, redacted or allowed a step, and why.

The OpenTelemetry generative AI conventions give vendor-neutral names for much of this, so traces from different frameworks line up.

Keep secrets out

Log the fact, not the secret. Redact tokens, passwords, API keys and card numbers before the line is written. Store a hash or a short prefix where you need to correlate. Treat the log as sensitive data itself: restrict access, set a retention period, and make it append-only so an attacker who controls the agent cannot rewrite history. The OWASP Logging Cheat Sheet covers each of these controls.

Before and after

Weak: INFO agent sent email. It names no recipient, no run and no cause.

Stronger:

{"ts":"2026-12-02T09:14:07Z","run_id":"r_81f2","step_id":"s_07","parent_step_id":"s_06","agent":"support-bot@4.2","user":"u_5521","tool":"send_email","args":{"to":"x@unknown-domain.example","subject_len":42,"body_sha256":"9c1e..."},"status":"approved","approved_by":"u_5521","sources_read":["https://docs.example/refund-policy#sha256=ab12..."],"cred":"svc_mail_ro"}

What goes wrong

  • Logging only the final answer. The tool calls and sources are where attacks show up.
  • Logging everything raw. Full prompts with customer data and pasted tokens turn the log into a breach target.
  • Logs the agent can write to or delete. An attacker with the agent's rights can erase the trail.
  • No clock or id discipline. Without shared ids and timestamps, steps from parallel tools cannot be ordered.

Examples

Reconstructing a bad email

A support agent emailed an internal refund policy to an outside address. The log shows run r_81f2, step s_07 called send_email with an unfamiliar domain. Its parent step s_06 read a web page whose content hash does not match the saved copy. The page held the injected instruction. Without sources_read, the team would have blamed the model and missed the poisoned page.

A minimal log record schema

interface AgentLogEvent {
  ts: string;
  runId: string;
  stepId: string;
  parentStepId?: string;
  actor: { userId: string; agent: string; credentialId: string };
  kind: "model_call" | "tool_call" | "source_read" | "approval" | "policy";
  tool?: string;
  args?: Record<string, unknown>; // redacted before writing
  status: "ok" | "error" | "blocked" | "approved" | "denied";
  sourceHash?: string;
}

One shape for every event kind keeps queries simple during an incident.

Key points

  • Give every run and step an id with a parent link so the whole chain can be rebuilt later.
  • Log every external source the agent read, with a hash of the content, because injected instructions arrive that way.
  • Record tool name, arguments, result status and the target of each side effect, not just the final answer.
  • Redact secrets before writing and log an id or hash instead of the token itself.
  • Make logs append-only and access-controlled, since the agent or an attacker may try to alter them.

Common mistakes

  • Logging only the model's final answer and not the tool calls and sources behind it.
  • Writing raw prompts, tokens or customer data into the log without redaction.
  • Letting the agent's own credentials write to or delete its audit log.
  • Skipping run and step ids, so parallel tool calls cannot be put in order.

Check yourself · 2 of 3

An agent calls a payments API with a bearer token. What should the audit log record about that credential?

Choose one answer

Check yourself · 3 of 3

Why should an agent's audit log be append-only and writable by a separate service?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic