Join our Newsletter — 33% off our NHI Course
Home› Guides› Secure AI logging: what to log for LLM…
Guide Agent Controls & Monitoring

Secure AI logging: what to log for LLM apps and agents without leaking data

← All guides
By NHI Mgmt Group, edited by Lalit Choda Updated 8 November 2026 14 min read
On this page

LLM applications and AI agents need a security audit trail, but the obvious way to build one, logging every prompt and response, creates a second copy of your most sensitive data in a system that was never designed to hold it. This guide sets out a security logging design for LLM apps and agents: the events to record, how to attribute actions to human and non-human identities, what to keep out of logs, how to protect logs from injection and tampering, and how long to keep them, including the record-keeping articles of the EU AI Act. It is for security architects, detection engineers and platform owners. Tracing, token metrics and quality evaluation are covered in the LLM Observability Guide and are not repeated here.

Key takeaways

  • Log decisions and actions by default, content by exception. Identity, tool calls, authorisation outcomes and guardrail verdicts belong in the security log; prompt and response text should be off, or held in a separate store with tighter access and shorter retention.
  • Every agent action needs two names. Record the agent's own identity and the human or service it acted for, otherwise downstream logs show the agent as the actor and the investigation stalls.
  • AI logs are an attack surface. Model output written to logs can forge entries (CWE-117), and log content read back by AI tools can carry prompt injection, so encode on write and treat log content as untrusted on read.
  • The never-log list grows with AI. Beyond passwords, tokens and card data, add system prompts, retrieved documents, tool results that echo secrets and raw embeddings.
  • Retention is a legal decision. The EU AI Act requires logs from high-risk systems to be kept for at least six months, while data protection law limits how long personal data can sit in them; set tiers per field, not per log.

Security logging versus observability

One shared store makes prompts readable by every engineer. Separate the purposes first.

PurposeQuestions it answersTypical storeContent needed?
Security auditWho invoked the agent, under which identity, which tools ran, what was allowed or deniedSIEM or security data lake, write-onceRarely: metadata and hashes
Incident investigationWhat exactly did the model see and produce in this sessionRestricted content store, referenced from the audit logYes, time-limited and access-controlled
Regulatory record-keepingTraceability of a high-risk AI system over its lifetimeAudit store with defined retentionDepends on the system; see the retention section

The OpenTelemetry GenAI semantic conventions make the same split. Three patterns are listed: the default of not recording instructions, inputs or outputs; recording them on span attributes, which the conventions describe as suited to cases where privacy regulations do not apply or storage complies with them, such as pre-production; and storing content externally with references on the spans, which they recommend for production where sensitive data must be handled securely, because external storage allows separate access controls.

What to log: the AI security event catalogue

OWASP frames every event as when, where, who and what, linked by an interaction identifier: for AI, the conversation ID, with a "who" of at least two parts.

EventKey fieldsWhy it matters
Session or conversation startConversation ID (gen_ai.conversation.id), user identity, client application, agent ID and version, model and provider, prompt template versionAnchors every later event; prompt and model versions explain behaviour changes
Model invocationOperation name, requested and actual model, response ID, finish reason, token counts, latencyAbuse, Denial of Wallet and key misuse show first as volume and model anomalies
Guardrail or classifier verdictWhich control ran (input filter, output filter, PII detector), verdict, category, action taken (block, redact, allow), confidencePrompt injection attempts and data leakage are visible here before any harm occurs
RetrievalIndex or data source, number of documents, document IDs or hashes, the identity used for the permission check, sensitivity labels of returned itemsShows what data the model was given and under whose access
Tool or function callTool name, call ID, target system, acting identity and on-behalf-of identity, argument hashes or redacted arguments, result statusThe point where an agent changes something; the core of agent accountability
Authorisation decisionPolicy evaluated, decision (permit or deny), scopes requested and granted, step-up or human approval requested and givenDenied calls and scope elevation are high-value detection signals
Human approvalApprover identity, what was shown to them, decision, time to decideProves human oversight happened and who exercised it
Configuration changeSystem prompt version, tool registry change, model swap, guardrail threshold change, MCP server added or removed, who made itSilent changes here alter behaviour more than any code release
Credential and token eventsToken issued to agent, audience, scopes, lifetime, revocation, secret retrieval by the agent runtimeLinks agent behaviour to the non-human identity lifecycle
Logging pipeline healthExporter failures, dropped spans, redaction processor errors, logging pausedOWASP lists logging start, stop and pause as events in their own right

OWASP warns that a blind checklist creates "alarm fog": start with tool calls, authorisation decisions, guardrail verdicts and configuration changes.

Attribution: recording who really acted

An agent that calls an API with its own credential, on behalf of a user, through an MCP server, produces logs in at least three systems. Unless each hop records both identities and a shared correlation ID, the downstream system sees only the last caller.

  • Agent identity: record a stable agent identifier. The OpenTelemetry agent conventions define gen_ai.agent.id and suggest the provider-assigned identifier for hosted agents, such as a cloud agent resource ARN.
  • Principal identity: record the human or upstream service the agent served, taken from the validated token or session, never from a field the model or client can set.
  • Delegation chain: where agents call agents, log the whole chain. See the Delegation Chains and Attenuated Authority for AI Agents.
  • No token passthrough: the MCP security best practices explain that when a server forwards a client's upstream token, it cannot tell clients apart and downstream logs show a different identity from the one actually acting. The MCP specification forbids the pattern: servers must not accept tokens that were not issued for them.
  • Scope elevation: the same MCP guidance tells servers to log elevation events, the scope requested and the subset granted, with correlation IDs.

Each agent also needs an owner, an inventory entry and traceable credentials. See the Agent Identity and Access Guide.

What to keep out of AI logs

Start from the OWASP never-log list (session IDs, access tokens, passwords, connection strings, keys, card data, sensitive personal data, source code). AI systems add categories that arrive inside larger payloads.

DataWhere it hidesDefault treatment in the security log
User promptsInput messages, chat history replayed on every turnDo not log; record length, language, classifier verdicts and a keyed hash
System promptsgen_ai.system_instructions, debug dumpsLog the template version ID only; OWASP LLM07:2025 System Prompt Leakage applies to your logs
Retrieved documentsContext assembled for retrieval augmented generationLog document IDs, source and sensitivity label, not text
Tool arguments and resultsgen_ai.tool.call.arguments and gen_ai.tool.call.resultAllowlist safe fields; redact the rest; tool results often echo API keys, records or file contents
Secrets pasted by usersAny free-text fieldScan and mask at the collector before export; alert on detection
EmbeddingsVector payloads in retrieval spansNever log raw vectors; they can be partially inverted to source text

Redact in the pipeline rather than in each SDK, so that every exporter receives the cleaned record. A fail-closed attribute allowlist beats a pattern blocklist, because new tools add fields without warning. For the legal basis, pseudonymisation and subject rights around security logs generally, see the Privacy in Security Logging and Monitoring Guide.

Protecting AI logs as an attack surface

Injection on write

Model output is attacker-influenced data. If it reaches a text log unencoded, carriage return and line feed characters can forge or split entries (CWE-117, log injection). OWASP recommends structured formats such as JSON, encoding values for the output format, sanitising delimiter characters and limiting the size of user-supplied values. It also notes that parameterised logging alone does not stop CR and LF injection: use a structured encoder that escapes values.

Injection on read

Logs are increasingly read by AI: SOC copilots summarise alerts, and coding agents fetch error reports through MCP servers. Anything an attacker can write into a log can become an instruction to the agent that reads it (MITRE ATLAS AML.T0051 LLM Prompt Injection). The Sentry MCP Agentjacking 2026 research showed a fabricated error report, written with a public write-only key, being returned to coding agents as tool output and acted on. The customer-side lesson is to treat tool output and log content as untrusted input, require approval before agents run commands drawn from external content, and keep long-lived secrets out of agent environments. See the Agentic AI in the SOC Guide.

Integrity and access

  • Tamper evidence: OWASP calls for tamper detection and moving logs to read-only media as soon as practical. Use write-once storage or hash chaining (AU-9, AU-10).
  • Least privilege on logs: restrict and periodically review who can read logs, and log access to logs. The restricted content store should be readable only by a named investigation role (AU-9(4)), with every read recorded.
  • Detect logging stopping: an attacker who controls an agent may try to disable its telemetry. Alert when an agent's event volume drops to zero or redaction processors start failing (ATT&CK T1685 Disable or Modify Tools).

Retention and record-keeping

Regulation (EU) 2024/1689, the EU AI Act, has three logging provisions for high-risk AI systems, as published on the European Commission's AI Act Service Desk (checked November 2026):

  • Article 12 (record-keeping): high-risk AI systems shall technically allow for the automatic recording of events (logs) over the lifetime of the system, enabling events relevant to identifying risk situations, post-market monitoring and monitoring of operation by deployers. Annex III point 1(a) biometric systems have a longer minimum list.
  • Article 19 (providers): providers keep the automatically generated logs under their control for a period appropriate to the intended purpose, of at least six months, unless Union or national law, in particular data protection law, provides otherwise.
  • Article 26(6) (deployers): deployers keep the logs under their control for at least six months on the same terms; deployers that are financial institutions keep them as part of the documentation required by financial services law.

The application dates of the high-risk obligations were amended by Regulation (EU) 2026/1744 (the Digital Omnibus on AI), in force since 27 July 2026; secondary summaries give 2 December 2027 for Annex III systems and 2 August 2028 for AI in products covered by EU product legislation. Confirm the dates against the official text before planning to them. See the EU AI Act High-Risk AI Systems Guide and the EU AI Act Implementation Timeline Tracker. OWASP's general rule applies either way: do not destroy logs before the required period ends, and do not keep them beyond it.

TierContentsExample retention approach
Audit metadataIdentities, tool calls, decisions, verdicts, configuration changesLongest: meets regulatory minimums (six months or more for high-risk systems) and your incident investigation window
Restricted contentPrompts, outputs, retrieved text for sessions under investigation or flagged by a guardrailShort and purpose-bound; extended only by a legal hold on named sessions
Engineering tracesSpans, latency, token metrics without contentOperational need only

How to implement

  1. Inventory AI systems and log paths: list every LLM app, agent, gateway and MCP server, where each sends telemetry today and whether content capture is on.
  2. Define the event schema: adopt the event catalogue above, mapped to OpenTelemetry GenAI attribute names where they exist, and add your own fields for on-behalf-of identity and authorisation decisions.
  3. Turn content off in production: set every instrumentation library to metadata only, then add a restricted content store with references for the use cases that justify it.
  4. Put redaction in the collector: deploy a fail-closed attribute allowlist and secret and personal data patterns at the telemetry collector. Test with seeded canary values (a fake API key, a test card number) and confirm none reach any backend.
  5. Pilot on one agent: check that an investigator can reconstruct a session from metadata plus the content store.
  6. Harden the stores: write-once audit storage, named access for the content store, access logging, and alerts on telemetry gaps.
  7. Roll out and set retention: extend agent by agent, apply the retention tiers with legal sign-off, and keep a rollback path (the previous logging configuration) until detections are validated.

Detection and measurement

Useful detections: repeated authorisation denials for one agent; tool calls outside its normal set; scope elevation followed by sensitive actions; guardrail blocks clustered on one user or document; secrets in prompts; out-of-window configuration changes; and telemetry that stops while the agent's credential is still in use.

  • Attribution coverage: percentage of tool-call events carrying both agent and principal identity (target 100%).
  • Content exposure: number of production systems exporting prompt or output text to a general-access backend (target zero).
  • Canary leakage: seeded canary values found in any log store per test cycle (target zero).
  • Reconstruction time: time for an investigator to rebuild a flagged agent session end to end.

Framework mapping

FrameworkReferenceHow it applies
NIST SP 800-53 Rev. 5AU-2, AU-3, AU-12Event selection, record content and generation for AI events
NIST SP 800-53 Rev. 5AU-9, AU-9(4), AU-10, AU-11Protecting logs, restricting privileged access, non-repudiation, retention
NIST CSF 2.0PR.PS-04, DE.CM-03Log records generated and available; activity monitored
OWASP Top 10 for LLM Applications 2025LLM02, LLM05, LLM07, LLM10Sensitive data in logs, output handling, system prompt leakage, consumption monitoring
MITRE ATLASAML.T0051, AML.T0057Prompt injection via log content; data leakage through logs

Common failure patterns

  • Framework defaults left on: an SDK or gateway captures full prompts because nobody changed the default, and the trace backend becomes the largest store of customer data in the company.
  • Agent shown as the only actor: logs record the agent's service account but not the user it served, so an investigation cannot tell a compromised user from a misbehaving agent.
  • Redaction in one place only: masking is configured in one SDK, while a second exporter ships the raw spans elsewhere.
  • AI trusting logs: an agent acts on text an outsider wrote into an error report.
  • One retention for everything: prompts are kept as long as audit records.

Practitioner checklist

  • Inventory every AI app, agent, gateway and MCP server and record whether prompt or output content capture is enabled.
  • Set content capture to off in production instrumentation and use a separate, access-controlled content store with references where content is justified.
  • Log tool calls, authorisation decisions, guardrail verdicts, human approvals and configuration changes with a conversation ID.
  • Record both the agent identity and the on-behalf-of principal on every tool call, taken from validated tokens.
  • Deploy fail-closed attribute allowlists and secret and personal data redaction at the collector, and test with canary values.
  • Write AI logs in a structured format with an escaping encoder, and treat log and tool output as untrusted when AI reads it.
  • Store audit logs write-once, restrict and log access to them, and alert when an agent's telemetry stops.
  • Agree retention tiers with legal, including the six-month minimum for high-risk systems under the EU AI Act where it applies.

Standards and references

Related NHI Mgmt Group resources: LLM Observability Guide · Privacy in Security Logging and Monitoring Guide · Agent Identity and Access Guide · AI data access review

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Researched and written by the NHI Mgmt Group editorial team using AI tools, under the editorial direction of Lalit Choda. Facts are drawn from the sources listed on this page and checked by our automated quality process. Last updated 8 November 2026. Spotted an error? Use our contact page.
NHI Mgmt Group, the independent authority on Non-Human Identity, IAM and Agentic AI security. nhimg.org