Zum Inhalt springen

AI Agent Decision Log: Why Did the Agent Do That?

In short

An AI agent orders 72 eggs and cannot explain why. The problem is not the order, it is the missing record. Here is how to build a decision log in n8n that makes every agent action traceable.

13 min read
AI Agents Traceability Logging n8n SMEs
AI Agent Decision Log: Why Did the Agent Do That?

Tuesday morning at the office: a customer calls because your agent sent them a quote with a discount that never existed. You ask the agent why it did that. It answers politely, plausibly and with three reasons. None of them can be found in any record. You now have no idea whether the agent made a mistake, whether someone redirected it with an email, or whether the answer simply sounds good.

In this article you will learn why an agent’s own explanation is not a record, which seven fields a usable decision log contains, how to build it in n8n with a handful of nodes, and how to get started within a week.


Table of contents

  1. The case of the 72 eggs
  2. Why the agent’s answer is not a record
  3. The seven fields of a decision log
  4. Building the logging in n8n
  5. Retention, privacy and regulation
  6. Rolling it out in one week
  7. Conclusion

The case of the 72 eggs

This week the German tech outlet t3n picked up a story that starts out funny and turns out to be surprisingly instructive. Analyst Matt Smith had his AI agent do the weekly grocery shopping. The agent ordered four packs of 18 eggs each, 72 in total. Smith asked why. According to the screenshot he shared, the agent answered that it had no good answer and could not find a note explaining why it had chosen four packs.

The second half is the interesting part. The agent went looking for possible explanations and guessed that it had misread the egg line in the photo of the shopping list. But there was no egg line on the list at all. Its conclusion: it had added them for no reason.

This is not an isolated incident. In the same piece, t3n recalls Meta researcher Summer Yue, whose agent started deleting every email while tidying up her inbox and ignored repeated commands to stop. The stories differ in the damage done, but not in the pattern: afterwards a human asks “why?”, and nobody can prove anything.

Policy is moving in the same direction. According to heise, 18 countries, ten companies (including Anthropic, Microsoft and OpenAI) and eleven non-governmental organisations signed the “Paris Call for AI safety and security” last week. The signatories name immediate results on transparency, incident reporting and risk management as a shared priority. It is a declaration of intent, not a law, but the direction is clear: if you run agents, you should be able to explain incidents.

Vendors are moving too. According to its own press release of 9 November, Cisco announced new agent observability capabilities. On 2 November, IBM announced that it is named a Leader in a new Gartner Magic Quadrant for AI Agent Management Platforms. When a market category for managing agents emerges, the topic has arrived in day-to-day business.

Why the agent’s answer is not a record

The reflex is understandable: when something goes wrong, you ask the agent. That works with a person, because a person remembers their reasons. A language model is different. When you ask, it has only the text in front of it that is part of the conversation. It does not read out what happened internally, it writes an explanation that fits that text.

Two consequences follow. First, the explanation can sound convincing and still be wrong. In the egg case, the agent was honest enough to say that no note existed. A different model with a different setup might have confidently produced three reasons. Second, the explanation changes when the context changes. Ask again tomorrow and you may get a different answer to the same question.

The agent’s sentence “I can’t find a note” is actually the real clue. The problem was not the agent, it was the system around it. There was no place where decisions were written down at the moment they were made. That is exactly what a log is: a record created at the time of the action that no longer depends on a model’s mood later.

You can put the distinction in one sentence. The agent’s explanation is a claim, the log is evidence. Both are useful, but only the log holds up when you talk to a customer, an insurer or your tax advisor.

If you have already learned to detect silent failures in n8n, you know the principle: monitoring tells you that something is off. The decision log tells you why.

The seven fields of a decision log

A log nobody can read is worthless, and one that stores too much becomes a privacy problem. These seven fields have proven to be the practical minimum:

  1. Run ID and timestamp. A unique number per run so you can join action, input and result.
  2. Trigger. What started the run: an email, a form, a schedule, another agent? Without this entry you will never find out whether someone steered the agent from outside.
  3. Input in short form. The task and the data used, for sensitive content as a reference or checksum instead of full text.
  4. Model and prompt version. Which model ran with which instruction. Without it you cannot tell after an update whether behaviour has changed.
  5. Intermediate steps. Every tool call with its parameters and result. This is the most valuable part: it shows what the agent looked up and what it relied on.
  6. Final action. What actually happened in the end: email sent, record changed, order placed. With target system and result.
  7. Approval. Did a human approve, who, and when? Or did the action run automatically, and under which rule?

On top of that comes an optional eighth field many people forget: the agent’s rationale before the action. In the instruction you require the agent to note, in a fixed field and in one sentence, what it intends to do and why before every write action. This field is still a claim by the model, not proof. But it has a value the after-the-fact explanation never has: it is created before the action and cannot be rewritten afterwards. Later you can compare the rationale against the actual intermediate steps. If they do not match, you have a finding.

Building the logging in n8n

In n8n you do not need an extra platform for this. The building blocks are there, they just have to be switched on deliberately.

Step 1: Make intermediate steps visible. The AI Agent node has an option called “Return Intermediate Steps”. When it is active, the agent’s output additionally contains a list of its tool calls with input and result. This is the raw material for field 5.

Step 2: Keep execution data. In the workflow settings you decide whether successful and failed executions are saved. Important for self-hosted n8n: automatic pruning of old executions is on by default, and according to the n8n documentation the default retention is 336 hours, which is 14 days (environment variables EXECUTIONS_DATA_PRUNE and EXECUTIONS_DATA_MAX_AGE). The execution list alone is therefore too short-lived for a decision log. It helps with debugging, but it is not an archive.

Step 3: Build the log row. Right after the agent, add a Code node that puts the seven fields into a fixed structure. An example:

// Code node "Build log row", runs directly after the AI Agent node
const agent = $input.first().json;
const trigger = $('Trigger').first().json;

const steps = (agent.intermediateSteps || []).map(s => ({
  tool: s.action?.tool,
  input: s.action?.toolInput,
  result: String(s.observation ?? '').slice(0, 500),
}));

return [{
  json: {
    run_id: $execution.id,
    workflow: $workflow.name,
    created_at: new Date().toISOString(),
    trigger_summary: JSON.stringify(trigger).slice(0, 300),
    model: 'enter-model-name-here',
    prompt_version: 'v3',
    agent_rationale: agent.rationale ?? null,
    steps,
    final_output: String(agent.output ?? '').slice(0, 1000),
    approved_by: null,
  },
}];

What the code does: it takes the agent’s output, truncates long content and returns a uniform row. You adapt the name of the trigger node and the rationale field to your workflow. The structure of the intermediate steps can differ slightly between n8n versions, hence the careful optional chaining.

Step 4: Write to a table. The row goes into storage that is not affected by pruning of executions: an n8n Data Table, a Postgres table or another database you already run. What matters is that the agent itself has no write access to this table. The log is written by the workflow, not by the model. Otherwise it is just another piece of text the model can influence.

Step 5: Log before write actions. For tools that change something (send email, change a record, place an order), you write the log row before the action and add the result afterwards. This is called a write-ahead log: even if the action crashes, the entry with the intent exists. The simplest way is to put this into a sub-workflow “Log action” that every write tool calls.

Step 6: Merge in failures. An error workflow with the Error Trigger node makes sure that aborted runs get an entry too, including the last known intermediate steps. These are exactly the cases you want to be able to explain later.

Practical tip: Write the log row before the action rather than after, because the runs that crash or get redirected are exactly the ones where you will need evidence later.

If you chain several agents together, pass the run ID through all stages. How that looks for agent teams is described in our article on multi-agent orchestration in n8n.

Retention, privacy and regulation

A decision log almost always contains personal data: names in emails, customer numbers, content from enquiries. That is why three rules belong in from the start.

Data minimisation. Store inputs as a short summary or a reference to the record, not as full text. For email content, the message ID plus a checksum is often enough, and later you can use the checksum to show that the content is unchanged.

A fixed retention period. Decide upfront how long logs are kept and delete automatically afterwards. Which period fits depends on the use case; for business-relevant transactions it is often driven by commercial and tax law. Clarify that with your tax advisor or data protection officer.

Limited access. Who may read the logs is a decision of its own. The decision log is an oversight tool and does not belong in the hands of everyone who has access to n8n.

On regulation: for high-risk systems, the EU AI Act requires automatic recording of events in Article 12 and, in the follow-up articles, retention of the logs for at least six months. Most agents in SMEs are not high-risk systems, but the principle is the same one you need anyway. If you build a log now, you will not need a second rebuild later. What else you should document is covered in the article EU AI Act for SMEs. This is not legal advice; for concrete obligations, involve a lawyer.

Rolling it out in one week

You do not have to rebuild all agents at once. Start with the agent that has the most external impact, meaning the one that sends emails, makes commitments to customers or changes records.

Day 1: Pick the agent and define the fields. Decide which agent you log first and create the table structure with the seven fields. One hour of work.

Day 2: Switch on intermediate steps. Enable “Return Intermediate Steps” and check what the output looks like in your n8n. Test with two or three sample runs.

Day 3: Log row and table. Add the Code node, adapt it and write the row to the table. Check that the agent has no write access to the table.

Day 4: Write-ahead for write tools. Build the “Log action” sub-workflow and attach it in front of every write action.

Day 5: Spot check. Pick three random runs from the past few days and try to explain what happened using only the log. If something is missing, add the field before you move on to the next agent.

After that, the log is a foundation for everything that is coming anyway: testing and evaluating agents works with the same data, and for emergencies you need both, the log and a kill switch. For a general overview of which tasks suit agents in the first place, see our guide AI agents for business.

Conclusion

The egg case is harmless because in the end only a fridge was full. But it shows cleanly where the real gap is: the agent did not answer from memory, it searched for a note, and there was none. Anyone running agents with access to email, customer data or payments needs that note beforehand, not when someone asks “why?”

A decision log consists of a few clearly defined fields, is written by the workflow rather than the model, and lives somewhere that outlasts the execution list. It costs one working day to set up per agent and saves hours in a dispute, hours you would otherwise spend guessing whether the agent, an attacker or your own prompt was at fault.

The first step is small: today, switch on “Return Intermediate Steps” for your most important agent and decide where the log row will be written. How we combine logging, approvals and a kill switch in one system is shown on the page about the B.R.A.I.N. AI system. For building with n8n, our services cover it.

About MadeByBrain: We build AI automation for SMEs: our own AI agents, n8n workflows and GEO strategies, live in production. From the first idea to productive automation.

Book a free strategy call →

Dieser Beitrag ist auch verfügbar in:

Related articles