Inferensys

Prompt

Agent Tool Error Log Enrichment Prompt Template

A practical prompt playbook for enriching raw agent tool errors with state, retry count, circuit state, and metadata before writing to production error logs.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
PROMPT PLAYBOOK

When to Use This Prompt

Understand the exact job this prompt performs, the required context, and the production scenarios where it should and should not be deployed.

This prompt is designed for platform reliability engineers and AI operations teams who need to transform a raw, unstructured tool exception into a structured, queryable, and enriched log record. The core job-to-be-done is closing the observability gap between a failed tool call and a useful diagnostic artifact. In production agent runtimes, a bare stack trace or error string lacks critical context: the agent's session, the retry count, the circuit breaker state, or whether the error message was truncated by an upstream proxy. This prompt sits in the error-handling path, consuming the raw failure and producing a JSON payload that your observability stack can index, alert on, and correlate with other traces.

The ideal user is an engineer integrating this prompt into an agent framework's exception handler. You need three pieces of context before this prompt is useful: the raw error object from the tool, the agent's current state (session ID, task ID, retry count, circuit state), and any known constraints about the tool's error output (e.g., a 1024-character truncation limit from a logging proxy). Without this context, the prompt will produce a valid JSON structure but with empty or placeholder fields, which defeats the purpose of enrichment. Do not use this prompt for real-time user-facing error messages; it is designed for machine-to-machine observability pipelines, not for explaining failures to end users.

This prompt is not a replacement for structured logging libraries in your tool code. If your tools already emit OpenTelemetry-compatible spans with full metadata, you do not need this enrichment step. Use it when you are consuming errors from third-party tools, legacy APIs, or sandboxed execution environments where you cannot modify the logging behavior. Additionally, avoid using this prompt for non-error telemetry, such as successful tool call traces or performance metrics; those require different schemas and should not be forced through an error-centric enrichment template. The next step after reading this section is to gather your raw error samples and agent state schema so you can populate the prompt's placeholders accurately.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Agent Tool Error Log Enrichment Prompt works and where it introduces risk. Use these cards to decide if this prompt fits your operational context.

01

Good Fit: Structured Incident Response

Use when: Your platform has a defined incident response process and SREs need enriched error logs to reduce mean-time-to-resolution. The prompt adds agent state, retry count, and circuit state to raw tool errors. Guardrail: Integrate the enriched log directly into your existing incident management channel to avoid creating a separate debugging silo.

02

Bad Fit: Unstructured Debugging

Avoid when: The goal is ad-hoc, exploratory debugging without a standard log schema. The prompt produces structured, machine-readable output that adds overhead if your team only uses raw text search. Guardrail: Use a simpler trace dump prompt for unstructured exploration and reserve this enrichment for automated alerting pipelines.

03

Required Inputs

Risk: The prompt cannot enrich what it cannot see. Missing agent state, truncated stack traces, or absent retry counts produce incomplete records that look valid but lack diagnostic value. Guardrail: Validate that the raw error payload contains the required fields before invoking enrichment. Reject and flag incomplete inputs rather than generating partial records.

04

Operational Risk: Sensitive Data Leakage

Risk: Enriching error logs with full agent state can inadvertently write user data, API keys, or PII into logging systems with broader access than the agent's runtime. Guardrail: Apply a sanitization layer before enrichment. Strip credentials, PII, and sensitive arguments from the agent state snapshot before it reaches the enrichment prompt.

05

Operational Risk: Log Volume Amplification

Risk: Enriching every tool error in a high-throughput system can multiply log storage costs and overwhelm log aggregation pipelines. Guardrail: Apply sampling or rate-limiting based on error type and severity. Enrich critical and unique errors fully; summarize or drop duplicate enrichment for repeated, low-severity failures.

06

Operational Risk: Stale Enrichment Context

Risk: Agent state captured at error time may be stale if the enrichment runs asynchronously or the agent has moved on to subsequent steps. Guardrail: Timestamp the agent state snapshot at capture time and include a staleness indicator in the enriched record. Discard enrichments where the state is older than a configured threshold.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt template for enriching raw agent tool errors with runtime context before writing to production error logs.

The prompt below is designed to be pasted directly into your error-handling path. It takes a raw tool error and enriches it with agent state, retry count, circuit state, and contextual metadata before the error is written to your observability system. Replace every square-bracket placeholder with runtime values from your agent execution context. The output is a structured JSON log record that your logging pipeline can index, alert on, and use for debugging multi-step agent failures.

text
You are an error log enrichment agent for a production AI system. Your task is to take a raw tool error and produce a structured, enriched error log record that includes agent state, retry context, circuit state, and diagnostic metadata.

## INPUT
Raw Tool Error: [RAW_ERROR]

## CONTEXT
- Agent ID: [AGENT_ID]
- Session ID: [SESSION_ID]
- Trace ID: [TRACE_ID]
- Tool Name: [TOOL_NAME]
- Tool Version: [TOOL_VERSION]
- Tool Arguments (sanitized): [TOOL_ARGS]
- Retry Count: [RETRY_COUNT]
- Max Retries: [MAX_RETRIES]
- Circuit State Before Call: [CIRCUIT_STATE]
- Agent State Snapshot (last 3 steps): [AGENT_STATE_SNAPSHOT]
- Timestamp (UTC ISO 8601): [TIMESTAMP]
- Environment: [ENVIRONMENT]

## CONSTRAINTS
- Do not fabricate missing fields. Mark unavailable fields as null.
- If the raw error message appears truncated, set `error_truncated` to true and note the truncation point.
- If no stack trace is present, set `stack_trace_available` to false and do not attempt to generate one.
- Preserve the original error message verbatim in the `raw_error` field.
- Classify the error category from this fixed list: TIMEOUT, AUTHENTICATION, AUTHORIZATION, RATE_LIMIT, INVALID_ARGUMENT, UPSTREAM_UNAVAILABLE, CIRCUIT_OPEN, UNKNOWN.

## OUTPUT_SCHEMA
Return a single JSON object with this exact structure:
{
  "enriched_error": {
    "trace_id": "string",
    "session_id": "string",
    "agent_id": "string",
    "tool_name": "string",
    "tool_version": "string",
    "timestamp": "string (ISO 8601)",
    "environment": "string",
    "error_category": "TIMEOUT | AUTHENTICATION | AUTHORIZATION | RATE_LIMIT | INVALID_ARGUMENT | UPSTREAM_UNAVAILABLE | CIRCUIT_OPEN | UNKNOWN",
    "raw_error": "string (original error verbatim)",
    "error_truncated": true,
    "stack_trace_available": false,
    "stack_trace": null,
    "retry_context": {
      "retry_count": 0,
      "max_retries": 0,
      "retries_exhausted": false
    },
    "circuit_state": "CLOSED | OPEN | HALF_OPEN",
    "agent_state_summary": "string (concise summary of last 3 agent steps)",
    "diagnostic_hints": ["string (actionable hint for the on-call engineer)"],
    "missing_fields": ["string (list of context fields that were unavailable)"]
  }
}

## INSTRUCTIONS
1. Parse the raw error for error category signals (e.g., 'timed out', '401', 'circuit breaker').
2. Check if the raw error message ends abruptly or contains truncation indicators.
3. Check if a stack trace is present in the raw error.
4. Determine if retries are exhausted based on retry_count vs max_retries.
5. Generate 1-3 diagnostic hints based on the error category, retry state, and circuit state.
6. List any context fields that were empty or unavailable in `missing_fields`.
7. Output only the JSON object. No markdown, no commentary.

Adaptation guidance: If your observability pipeline expects OpenTelemetry-compatible attributes, add span context fields to the output schema. If you log to a structured logging system like Splunk or Datadog, flatten the JSON to top-level key-value pairs. For high-compliance environments, add a redaction_audit field documenting any PII or secret removal performed before enrichment. Always test this prompt with truncated errors, missing stack traces, and circuit-open states before deploying to production error paths.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the Agent Tool Error Log Enrichment Prompt Template. Every variable must be populated before the prompt is assembled and sent. Missing or null values will cause enrichment gaps in the error log record.

PlaceholderPurposeExampleValidation Notes

[RAW_ERROR]

The original error object, message, or stack trace from the tool invocation failure

{"error": "ConnectionRefusedError", "message": "dial tcp 10.0.1.5:8080: connect: connection refused", "stack": "..."}

Must be a non-empty string or JSON object. If truncated, set [ERROR_TRUNCATED] to true. Parse check: confirm error.message field exists or extract first 512 chars.

[TOOL_NAME]

The canonical name of the tool that failed, matching the agent's tool registry

billing_api.get_invoice

Must match a registered tool name exactly. Validate against tool registry schema. If tool name is unknown, set to "unknown_tool" and flag for registry audit.

[TOOL_VERSION]

The version identifier of the tool at the time of failure

v2.3.1

Must be a non-empty string. If version is unavailable, set to "unversioned" and log a missing-version warning. Schema check: semver preferred but not required.

[AGENT_STATE]

Snapshot of the agent's current task, plan step, and active context at failure time

{"task_id": "inv-442", "step": 3, "plan": "reconcile_invoice", "active_context_keys": ["customer_id", "invoice_date"]}

Must be valid JSON. Include task_id, current_step, and active_context_keys at minimum. Null allowed if agent state is unrecoverable, but flag as degraded enrichment.

[RETRY_COUNT]

Number of times this tool call has been retried before this failure

2

Must be an integer >= 0. If retry count is unknown, set to -1 and flag for retry-tracker audit. Validate: retry_count must not exceed [MAX_RETRIES] from tool config.

[CIRCUIT_STATE]

Current state of the circuit breaker for this tool dependency

OPEN

Must be one of: CLOSED, OPEN, HALF_OPEN, or UNKNOWN. If circuit breaker is not implemented, set to "NOT_CONFIGURED". Validate against circuit breaker registry if available.

[SESSION_ID]

Unique identifier for the agent session or trace that owns this tool call

sess_8a7b3c2d-4e5f-6a7b-8c9d-0e1f2a3b4c5d

Must be a non-empty string. Validate UUID format if your system uses UUIDs. If session ID is missing, generate a recovery session ID and flag for trace correlation gap.

[ERROR_TRUNCATED]

Boolean flag indicating whether the raw error was truncated before enrichment

Must be true or false. If true, append truncation metadata to the enrichment record. If null, treat as false but log a missing-flag warning.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the Agent Tool Error Log Enrichment prompt into an agent's error-handling path with validation, retries, and safe logging.

This prompt is designed to sit inside an agent's exception handler, not as a standalone debugging script. When a tool call fails—whether due to a network timeout, a malformed response, or a permission error—the raw error object is often too sparse for diagnosis. The prompt enriches that error with the agent's current state, retry count, circuit breaker status, and contextual metadata before the record is written to your observability stack. The goal is to make every error log entry self-contained enough that an SRE can begin an investigation without hunting down separate session traces.

To wire this in, intercept the error at the tool execution boundary. Capture the raw error object, the tool's input arguments, the agent's current task ID, the session ID, the retry count for this specific tool invocation, and the circuit breaker state if you're using one. Pass these as structured inputs to the prompt. The model should receive a clear schema for the enriched log record, including fields like error_category, root_cause_hypothesis, missing_context_flags, and recommended_investigation_steps. Validate the output against this schema before writing it to your log store. If validation fails, fall back to a minimal structured log with the raw error and a prompt_enrichment_failed flag—never drop the error entirely. For high-severity errors (e.g., permission denials, data access failures), route the enriched record to a human review queue in addition to the log store.

Model choice matters here. Use a fast, cheap model for enrichment because this prompt runs on the error path and you don't want to add latency or cost to an already-degraded request. A small model like GPT-4o-mini or Claude Haiku works well. Set a tight timeout on the enrichment call—if the model doesn't respond within 2 seconds, log the raw error and move on. Never retry the enrichment prompt itself; if it fails, you still have the raw error. The enriched log is a value-add, not a critical-path dependency. Finally, ensure your logging pipeline redacts sensitive fields from tool arguments before they reach the prompt, using a separate sanitization step or a dedicated redaction prompt from the Tool Observability pillar.

IMPLEMENTATION TABLE

Expected Output Contract

Fields, format, and validation rules for the enriched error log record produced by the Agent Tool Error Log Enrichment Prompt Template.

Field or ElementType or FormatRequiredValidation Rule

error_id

UUID string

Must be a valid UUID v4. Reject if missing or malformed.

timestamp

ISO 8601 UTC string

Must parse to a valid UTC datetime. Reject if timezone is missing or offset is non-zero.

agent_id

string

Must match the pattern agent-[a-z0-9]+. Reject if null or empty.

tool_name

string

Must be a non-empty string matching a registered tool name in the tool registry. Reject if not found.

raw_error

object

Must contain at least message and type fields. Reject if message is truncated (ends with '...' or is under 10 chars).

retry_count

integer

Must be a non-negative integer. Reject if negative or non-numeric.

circuit_state

string

Must be one of: CLOSED, OPEN, HALF_OPEN. Reject on any other value.

agent_state_snapshot

object

Must contain task_id and step_number fields. Reject if task_id is null. Warn if step_number is missing.

PRACTICAL GUARDRAILS

Common Failure Modes

What breaks first when enriching tool error logs and how to guard against it.

01

Missing Agent State Context

What to watch: The enrichment prompt receives a raw error but no agent state, causing the log to lack critical debugging context like the current task, previous tool calls, or memory state. Guardrail: Require a structured [AGENT_STATE] input block with task ID, step number, and recent action history. Validate its presence before calling the enrichment prompt.

02

Truncated Stack Traces

What to watch: The raw error contains a truncated or incomplete stack trace, leading to an enriched log that misidentifies the root cause or omits the originating call. Guardrail: Add a pre-processing check that measures stack trace depth. If below a minimum threshold, flag the log with stack_trace_truncated: true and append the raw error payload to prevent information loss.

03

Hallucinated Retry or Circuit State

What to watch: The model infers or fabricates retry counts and circuit breaker states when they are not explicitly provided, creating a false reliability narrative in the error log. Guardrail: Supply explicit [RETRY_STATE] and [CIRCUIT_STATE] fields in the prompt template. Instruct the model to use the value "unavailable" if the input field is null, rather than guessing.

04

Inconsistent Log Schema Drift

What to watch: The model produces a valid JSON log but varies field names, nesting, or data types across different error types, breaking downstream log parsing and dashboards. Guardrail: Provide a strict [OUTPUT_SCHEMA] with required fields, types, and enum values. Use a post-generation JSON Schema validator and reject or repair non-conforming outputs before writing to the log stream.

05

Sensitive Data Leakage in Enriched Fields

What to watch: The enrichment prompt inadvertently copies API keys, PII, or raw request bodies from the error context into human-readable summary fields. Guardrail: Apply a sanitization step to the [RAW_ERROR] input before it reaches the prompt. Use a deny-list pattern to redact known sensitive headers and keywords, and add a final regex scan on the generated summary field.

06

Root Cause Misattribution in Multi-Tool Chains

What to watch: An error in a downstream tool is enriched with context from the immediate caller, but the true root cause is an upstream data corruption three steps earlier. Guardrail: Include a [CAUSAL_TRACE] input that maps the sequence of tool calls and their data dependencies. Instruct the model to distinguish between the triggering_error and the root_cause_event in the enriched output.

IMPLEMENTATION TABLE

Evaluation Rubric

Use this rubric to test the Agent Tool Error Log Enrichment Prompt before deploying to production. Each criterion targets a specific failure mode common to enrichment tasks, including hallucinated metadata, missing stack traces, and broken JSON structure.

CriterionPass StandardFailure SignalTest Method

Schema Compliance

Output is valid JSON matching the [OUTPUT_SCHEMA] exactly; all required fields present

JSON parse error, missing required field, or extra undeclared key

Automated schema validator run against 50+ test cases with malformed inputs

Error Preservation

Original error message and stack trace are preserved verbatim in [ORIGINAL_ERROR] and [STACK_TRACE] fields

Truncation, rewriting, or summarization of the raw error text

Diff check between input error string and output field; flag any character-level mismatch

Metadata Accuracy

[AGENT_STATE], [RETRY_COUNT], and [CIRCUIT_STATE] match the provided context exactly

Hallucinated retry count, invented circuit state, or agent state from wrong session

Assertion check comparing output values to ground-truth context variables

Truncation Detection

[TRUNCATION_DETECTED] is true when input error ends mid-word or exceeds [MAX_ERROR_LENGTH]; false otherwise

False negative on obviously truncated input; false positive on clean input

Boundary test set: 10 truncated errors, 10 complete errors, 5 edge cases at exact limit

Contextual Enrichment

[ENRICHMENT_NOTES] adds useful debugging context without fabricating root cause

Notes claim a specific root cause not present in evidence; notes are empty when context is rich

Human review of 20 enriched logs; flag any unsupported causal claim

Null Handling

Missing optional context fields are output as null, not as empty string or 'N/A'

Empty string, 'unknown', or 'N/A' used instead of null for missing data

Automated scan for forbidden placeholder strings in nullable fields

Timestamp Format

[ENRICHED_AT] is ISO 8601 UTC with millisecond precision

Wrong timezone, missing milliseconds, or relative timestamp like 'just now'

Regex validation against ISO 8601 pattern; timezone assertion check

Idempotency

Same input twice produces identical enrichment except for [ENRICHED_AT] timestamp

Different [ENRICHMENT_NOTES], different null handling, or non-deterministic field values

Run same test case 5 times; assert all fields except timestamp are byte-identical

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base enrichment fields but relax strict schema enforcement. Use a simpler output format (key-value pairs or markdown table) instead of full JSON. Focus on getting the enrichment logic right before adding validation layers.

code
Enrich this tool error with agent state:
- Error: [RAW_ERROR]
- Agent step: [CURRENT_STEP]
- Retry count: [RETRY_COUNT]
- Tool name: [TOOL_NAME]

Add any context you can infer. Output as markdown.

Watch for

  • Missing stack traces going unnoticed
  • Truncated error messages passed through without flagging
  • No distinction between tool errors and agent logic errors
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.