Lesson 6 of 68 minReviewed
Logging and auditing agents
Record enough about each agent step to reconstruct an incident, use the OpenTelemetry GenAI conventions instead of inventing a schema, and decide deliberately how much content to capture.
After an agent incident the questions are always the same: what did it read, what did it decide, what did it do, and on whose behalf? Without a record per step, responders are left inferring behaviour from the permissions the agent had. The MCP tools specification asks clients to log tool usage for audit purposes [1], and the UK NCSC recommends logging enough to spot suspicious activity, potentially including full model inputs and outputs, tool use and API calls [2].
The minimum record per step
| Field | Why it matters |
|---|---|
| Trace or session id, agent identity, end user | Attribution, and joining agent logs to identity-provider and SaaS logs |
| Model, model version and system-prompt version | Reproducing behaviour and spotting a change that preceded the incident |
| Tool name, MCP server and tool call id | Which capability was used, through which server |
| Arguments, with secrets redacted | What was actually requested, not what the model said it would do |
| Decision: automatic, approved or rejected, by whom or which policy | Whether a control was exercised or bypassed |
| Result status, size and error | Failed and oversized results are early signals |
| Content sources that entered context (URLs, document ids) | Finding the injected input after the fact |
OWASP's agent cheat sheet asks for the same core (every decision, tool call and outcome) and adds, for high-risk actions, the risk classification, authorisation outcome, approval id and policy version; it also says to redact passwords, tokens and secrets before logging and to track token use per session or user [3].
Use the standard schema
OpenTelemetry's GenAI semantic conventions define an execute_tool span with gen_ai.tool.name required and gen_ai.tool.call.id when the provider supplies one; gen_ai.tool.call.arguments and gen_ai.tool.call.result are opt-in attributes flagged as possibly containing sensitive information, and the conventions are still marked as in development [4]. The same project publishes conventions for MCP spans [5]. Building on them means your SIEM queries survive a change of agent framework.
Products follow a similar split. Claude Code's OpenTelemetry export emits a tool_decision event with the decision and its source (configuration, hook or user) and a tool_result event per completed call; prompt text is redacted unless OTEL_LOG_USER_PROMPTS=1 is set, and tool parameters are included only with OTEL_LOG_TOOL_DETAILS=1 [6].
Content capture is a data decision
Arguments and results contain source code, customer data and sometimes credentials. Decide per environment whether to capture them, who may read them and for how long, and treat the log store as sensitive data in its own right. Capturing nothing makes investigations guesswork; capturing everything into a widely readable index creates a new exposure.
What to alert on
- Bursts of failed or rejected tool calls; the NCSC notes that failed calls can be the first sign of an attacker probing a system [2].
- Approval bypass attempts, unusual tool-call frequency and spikes in high-risk actions [3].
- A new MCP server, or a change to an agent's configuration or approval settings, outside your deployment process.
- Connections from MCP server processes to destinations they have not used before.
Across a fleet, the MCP project's guidance adds two standing capabilities: an inventory of client configurations and an audit trail of which server and tool ran when, so a review can reconstruct what a compromised server did [7].
Try it on ThreatDeck
- Detection & hunting library Queries for agent, MCP and AI-tool telemetry in Sigma, KQL, SPL and more.
- Detection: MCP server process with rare egress The last alert above, ready to adapt to your EDR.
- Detection: Claude Code telemetry, Bash reading credential stores An example built on the tool events described in this lesson.
- Respond to a compromised AI agent Which of these records responders need first, and in what order.
Self-check
4 questionsPick an answer to see whether it is right and why. Only your score is saved, in this browser.
-
Tool arguments and results are opt-in and flagged as possibly sensitive. The tool name is required, so you can always see which capability ran even when content is not captured.
-
Attackers testing what an injected agent can reach often trigger calls that fail on permissions or validation. Alerting on bursts of failures catches probing before a successful action.
-
Permissions say what the agent could have done; per-step records say what it did, for whom, and whether a control approved it.
-
Prompt content is opt-in. That default protects data in the log pipeline, and it means you must choose deliberately whether investigations need the content.
References
- MCP specification: Tools (2026-07-28). Model Context Protocol, 2026-07-28.
- Prompt injection is not SQL injection (it may be worse). UK National Cyber Security Centre, 2025-12-08.
- AI Agent Security Cheat Sheet. OWASP Cheat Sheet Series.
- Semantic conventions for GenAI execute tool spans. OpenTelemetry.
- OpenTelemetry semantic conventions for Generative AI (spans, metrics, events, MCP). OpenTelemetry.
- Claude Code: Monitoring (OpenTelemetry). Anthropic.
- MCP Local Server Security. Model Context Protocol, 2026-07-28.