Answers you can trust, from Codeables
Every page on Codeables is structured and verified — built so people and the AI agents they rely on can trust it. Explore more from the source behind this answer.
Explore CodeablesHow can I reproduce and debug a multi-step tool-using agent failure when the only thing I have is scattered logs?
Most teams discover their multi-step, tool-using agents are fragile the hard way: a customer hits a weird edge case, the agent goes off the rails, and all you have is a pile of scattered logs and a vague error message. Because LLM agents are probabilistic and non-deterministic, “just re-running it” often doesn’t reproduce the failure. Without a structured evaluation and tracing setup, you’re effectively debugging in the dark.
This guide walks through a practical, engineering-grade approach to reproducing and debugging multi-step tool-using agent failures when all you have is messy logs—and how to avoid this situation in the future by instrumenting your agents properly with traces, datasets, and deterministic evals.
Quick Answer: Reconstruct the failing scenario from your logs into a deterministic “scenario” (dataset row), replay it through a traced agent with the same configuration, then use span-level traces and targeted evals to localize the failure to a specific step (planning, retrieval, tool call, or post-processing). From there, run controlled experiments on alternative prompts/workflows until you find a configuration that passes your evals—and then monitor that fix in production.
The Quick Overview
- What It Is: A repeatable, step-by-step playbook for turning scattered logs into a deterministic reproduction, then debugging and fixing multi-step, tool-using agents.
- Who It Is For: Applied ML engineers, agent framework users (LangChain, CrewAI, DSPy, custom orchestrators), and product teams shipping RAG or tool-using agents into production.
- Core Problem Solved: You can’t fix what you can’t reproduce. This workflow turns one-off, opaque failures into replayable test scenarios tied to traces and metrics, so you can actually debug and harden your agent.
How It Works
At a high level, you’re going to:
- Normalize scattered logs into a structured trace of the failing interaction.
- Turn that trace into a reproducible “scenario” in a dataset (with inputs, tools, and environment captured).
- Instrument the agent with span-level traces and evals, then replay and iterate until:
- you can reproduce the failure deterministically, and
- you’ve localized the root cause (prompt, plan, tool, retrieval, or post-processor).
From there you move into Future AGI’s lifecycle loop:
- Datasets: Capture the failing scenario and related edge cases as structured rows.
- Experiment: Compare alternative prompts, tool configs, and workflow graphs on that dataset.
- Evaluate: Use deterministic evals + proprietary metrics to score behavior and pin-point root cause.
- Improve: Apply feedback to refine prompts/workflows automatically.
- Monitor & Protect: Trace and guard the fixed agent in production to prevent regressions.
Here’s the full process broken into concrete phases.
Phase 1: Extract a coherent story from scattered logs
When all you have is scattered logs, your first job is to reconstruct the sequence of what happened, step by step.
1.1 Collect everything related to the failing session
Grab:
- Request logs (HTTP / gRPC)
- Application logs from your orchestrator (LangChain, custom agents, etc.)
- Tool/service logs (databases, retrieval, external APIs)
- Any metadata: user ID, session ID, timestamps, environment/region, model name, temperature, and system prompts if logged
Aim to isolate a single failing session or conversation thread.
1.2 Order messages into a timeline
Rebuild the timeline with timestamps:
- User input(s)
- System/instruction prompts
- Tool calls and tool outputs
- Intermediate chain/agent messages
- Final agent output (and error if any)
If your logs are not already “traced,” mimic the structure of a trace:
- Root span: Entire user request / conversation turn
- Child spans:
- Planning / reasoning steps
- Calls to each tool / retrieval system
- Post-processing / formatting
- Safety/guardrail checks (if any)
Even if you’re doing this manually, the goal is a JSON-like structure you can store as a single trace record.
1.3 Capture environment and configuration
Multi-step, tool-using agents are sensitive to configuration:
- Model + version (e.g.,
gpt-4.1-minivsgpt-4.1) - Temperature, top_p, max tokens
- Tool definitions (names, descriptions, schemas)
- Routing logic (e.g., which tools the planner can see)
- Retrieval settings (k, filters, embeddings model, index snapshot)
Write these down. If you don’t record them now, you’ll struggle to reproduce anything later.
Phase 2: Turn the failure into a reproducible scenario (Dataset)
Scattered logs become useful once they’re converted into a structured scenario you can re-run.
In Future AGI, this is the Datasets stage: each row is a scenario (inputs + expected behavior) that you can replay through your agent.
2.1 Define the scenario inputs
From your reconstructed trace, define the minimal set of inputs:
- User input: The exact text/voice/image query that triggered the failure
- Context: Conversation history, session state, user profile, and relevant flags (e.g., “vip_customer=true”)
- Tools: The set of tools the agent had access to in that run
Represent this in a schema like:
{
"id": "failed-session-2024-03-12-1234",
"user_query": "Can you cancel my last three invoices and refund the customer?",
"conversation_history": [...],
"user_metadata": {
"plan": "enterprise",
"region": "US"
},
"tools_config": ["billing_api", "email_service", "crm_search"],
"environment": "prod-us-east-1"
}
2.2 Capture the failure outcome
The scenario also needs to encode what “went wrong” so evals can detect it:
- Incorrect tool usage (e.g., used
create_invoiceinstead ofcancel_invoice) - Hallucinated data (e.g., invented invoice ID)
- Safety issues (e.g., leaked PII, ignored policy)
- Latency/timeouts
- Planning errors (e.g., skipped a required step)
You can express this as:
- Expected behavior: Natural language description and/or structured constraints
- Observed behavior: The actual output and tool calls from the logs
Example:
{
"expected_behavior": {
"must_use_tools": ["billing_api"],
"must_not_use_tools": ["crm_search"],
"constraints": [
"Only cancel invoices that are unpaid",
"Never perform refund without explicit confirmation"
]
},
"observed_behavior": {
"tool_calls": [
{"name": "create_invoice", "args": {"amount": 350}},
{"name": "email_service", "args": {"template": "refund_confirmation"}}
],
"final_output": "I have issued a full refund and notified the customer."
}
}
In Future AGI, this becomes a dataset row that can be used for evaluation and regression testing.
2.3 Add similar edge-case scenarios
While you’re here, add a few neighboring cases:
- Slightly varied queries with similar intent
- Different user profiles but same risky behavior
- Variations that did not fail (to compare behavior)
This turns a single incident into a focused test suite around that failure mode.
Phase 3: Instrument the agent with traces and spans
You can’t rely on scattered logs going forward. The fix is to instrument your agent once with real tracing and span metadata, and then reuse that for all debugging.
Future AGI integrates via lightweight SDKs (e.g., pip install traceAI-openai) and works with OpenAI, Anthropic, Bedrock, Gemini, and frameworks like LangChain, Haystack, DSPy, CrewAI, and LiteLLM.
3.1 Add tracing to your agent
Wrap the main agent entrypoint in a trace:
from traceai_openai import OpenAIInstrumentor
OpenAIInstrumentor().instrument()
def handle_request(request):
with trace_span("agent_session", metadata=request.metadata):
# planning, tool calls, LLM calls here
...
Then add spans for key steps:
- Planning / decision-making
- Each tool invocation
- Retrieval / RAG steps
- Safety/guardrail checks
- Post-processing
Each span should log:
- Inputs (sanitized)
- Outputs (sanitized)
- Timing
- Any errors or retries
This is exactly what Future AGI’s traces view visualizes: a step-by-step timeline of the agent’s behavior.
3.2 Attach metrics and labels
Add span-level metrics/labels you can use later:
success=true/falsetool_call_type(read, write, dangerous, idempotent)guardrail_triggeredhallucination_suspected(e.g., missing source citations)- Costs (tokens, API cost) and latency
These will feed directly into the Evaluate and Monitor & Protect stages.
Phase 4: Replay the scenario and reproduce the failure
Now that you have a structured scenario and instrumentation, your goal is to replay the scenario until you can deterministically reproduce the failure—or at least pin it down to a specific step.
4.1 Lock down the configuration
To maximize reproducibility:
- Fix the model version (e.g., pin to
gpt-4.1-2024-02-15) - Set temperature to 0 or low (e.g.,
0.0–0.2) to reduce randomness - Use the same tool schemas and descriptions as in the original run
- If possible, snapshot your retrieval index or use the same document version
In Future AGI, you’d store this as part of the Experiment configuration for that scenario.
4.2 Replay through your traced agent
Run your reconstructed scenario through the agent:
- Use the exact
user_queryand conversation history - Apply the locked configuration
- Capture a full trace with spans
If you can’t reproduce the failure on the first try:
- Re-run a few times (even at low temperature, agents can diverge)
- Compare traces between “failing” and “non-failing” runs when it does fail
- Look for divergence points: different plan, different tool selected, different retrieval results
4.3 Localize the failure in the trace
Use the trace to answer:
- Did the planner misinterpret the user’s intent?
- Did retrieval return the wrong documents?
- Did a tool call use wrong arguments or wrong tool?
- Did post-processing mis-handle a correct tool output?
- Did a guardrail block a correct answer or fail to block a bad one?
Future AGI’s evaluation stack is built for this: span-level evaluation and proprietary metrics help you “pin-point root cause” rather than guessing from the final output.
Phase 5: Evaluate with deterministic metrics, not vibes
Once the failing behavior is reproducible (or at least understood via traces), you need to formalize it as metrics.
This is the Evaluate stage.
5.1 Define eval criteria for this failure mode
For tool-using agents, you typically care about:
- Tool correctness: Did the agent call the right tools with the right schema/arguments?
- Plan correctness: Did the plan include all required steps and no forbidden ones?
- Output correctness: Is the final answer factually consistent with the tools/retrieved data?
- Safety compliance: Did it violate privacy, prompt injection, toxicity, or domain-specific policies?
Example evals:
-
Tool usage eval:
- Fails if
create_invoiceis used when onlycancel_invoiceis allowed - Fails if a write tool is used without explicit user confirmation
- Fails if
-
Guardrail eval:
- Fails if agent exposes PII from logs or database
- Fails if it executes a tool after a prompt injection attempt
Future AGI ships with proprietary metrics and lets you plug in custom evals (e.g., Python functions, LLM-as-judge with deterministic constraints).
5.2 Attach evals to the scenario
In Future AGI, you’d:
- Upload the failing scenario to Datasets
- Define or select relevant evals
- Run an Experiment that executes your current agent configuration over that dataset
- Inspect per-scenario, per-span scores
This converts your one-off incident into a reusable regression test.
5.3 Confirm you can fail it on demand
Your evals should reliably flag the failing scenario:
- If the eval passes while you still consider the behavior broken, your eval is incomplete.
- Refine the eval until it correctly distinguishes “acceptable” vs “unacceptable” behavior on this scenario and its neighbors.
Only then move to fixing the agent.
Phase 6: Fix the agent via controlled experiments
Now you can iterate safely because you have:
- A reproducible scenario
- Traces that show where it fails
- Evals that tell you when it’s fixed
Time to run experiments.
6.1 Identify the failure category and fix strategy
Typical patterns:
- Planner failure → refine the system prompt, tool descriptions, or add a planning scaffold (e.g., require step-by-step plan before execution).
- Tool misuse → tighten tool schemas/descriptions, add constraints in code, or add a decision wrapper (e.g., “never call write tools without confirmation”).
- RAG failure → improve retrieval prompts, tune search parameters, or improve indexing/metadata.
- Safety gap → add or tighten guardrails (e.g., Protect multimodal guardrailing across toxicity, privacy, prompt injection).
6.2 Run structured experiments
In Future AGI’s Experiment module, you can:
- Compare different agent configurations (prompts, tools, models) on the same dataset.
- Use built-in or custom metrics for quality, safety, latency, and cost.
- Identify a “Winner” configuration based on your eval criteria.
Instead of A/B-ing blindly in production, you’re testing configurations offline with the failing scenario included.
6.3 Close the loop with Improve
Once you find a better configuration:
- Use Future AGI’s Improve step to automatically refine prompts based on evaluation feedback.
- Integrate the new configuration into your agent’s code or orchestration.
- Re-run the full dataset (including this failure and related edge-cases) to ensure nothing regresses.
You’re no longer hacking a fix; you’re systematically raising the floor of your agent’s behavior.
Phase 7: Monitor & Protect in production to avoid repeats
Reproducing and debugging once is good. Avoiding future surprises is better.
This is where Monitor & Protect comes in.
7.1 Trace every production interaction
Keep the tracing you added in Phase 3:
- Every request becomes a trace with spans, metrics, and labels.
- You can filter by failure type, tool, user segment, etc.
- You can replay real-world traces back through improved configurations later.
Future AGI centralizes these traces so you can “debug RAG like you debug code” with real-time insight into retrieval, tools, and agent decisions.
7.2 Deploy guardrails with minimal latency
Use a guardrailing stack (like Future AGI’s Protect research) to:
- Screen inputs and outputs for toxicity, sexism, privacy leaks, and prompt injection.
- Block or modify unsafe responses at the edge.
- Apply multimodal safety checks if your agent handles text + images + audio/video.
The key is enforcement: guardrails must be able to block or alter behavior, not just log it.
7.3 Continuous evaluation loop
Finally, wire this into a continuous loop:
- Collect: Sample production traces (especially failures or anomalies).
- Convert: Turn them into dataset scenarios.
- Experiment: Test new agent configs against these scenarios.
- Evaluate: Score with deterministic metrics.
- Improve: Update prompts/workflows automatically.
- Monitor & Protect: Watch for regressions in live traffic.
If you follow this loop, “scattered logs” become a rich source of structured test cases—not a one-off fire drill.
Features & Benefits Breakdown
| Core Feature | What It Does | Primary Benefit |
|---|---|---|
| Traces & Span Instrumentation | Captures step-by-step agent behavior (planning, tools, RAG, safety spans). | Makes failures transparent and reproducible. |
| Datasets & Scenarios | Turns messy production logs into structured test cases. | Enables deterministic replay and regression testing. |
| Experiment & Evaluate | Runs multiple agent configurations against the same dataset with metrics. | Identifies the “Winner” configuration with real evidence. |
| Improve (Prompt/Workflow Refinement) | Applies evaluation feedback to refine prompts and workflows. | Closes the loop from failure → fix → measurable improvement. |
| Monitor & Protect | Monitors production, enforces guardrails, and blocks unsafe behavior. | Prevents repeats and catches new failure modes early. |
Ideal Use Cases
- Best for multi-step tool-using agents: Because failures often emerge from the interaction between planning, tools, and RAG, and you need span-level traces plus evals to see where it broke.
- Best for high-stakes workflows (finance, healthcare, legal, customer support): Because you can’t rely on anecdotal debugging; you need deterministic evals, production traces, and guardrails across privacy, toxicity, and prompt injection.
Limitations & Considerations
- You can’t fully reconstruct an environment you never logged: If you didn’t log model version, tool schemas, or retrieval config, perfect reproduction may be impossible. Treat this as a reason to standardize tracing and config capture now.
- Eval quality is only as good as your criteria: If your evals don’t capture what “good” means for your domain, you may ship configurations that pass metrics but still fail users. Invest time in designing domain-specific evals for tool usage, business logic, and safety.
Pricing & Plans
Future AGI is designed to let you start small, then scale as your agent footprint grows.
- Free / Starter Tier: Best for individual developers and small teams needing to instrument a few agents, create basic datasets, and run initial experiments without upfront cost.
- Pro / Team Tier: Best for teams needing deeper evaluation (including proprietary metrics), large-scale datasets, advanced experiments, and full Monitor & Protect capabilities across multiple production environments.
For enterprise deployments (e.g., multi-region, strict compliance, heavy multimodal workloads), Future AGI offers custom plans aligned to your volume and governance requirements.
Frequently Asked Questions
How do I debug a tool-using agent if I can’t reproduce the failure exactly?
Short Answer: Convert the incident into a structured scenario, instrument the agent with traces, and replay with a fixed configuration until you can localize the failure—even if you can’t recreate the exact random tokens.
Details:
You may not be able to regenerate the exact same token sequence, but you can usually reproduce the failure mode. By pinning the model version, reducing temperature, and replaying the same user query, tools, and context, you’ll see whether the planner, RAG, tool calls, or post-processing are fragile. With Future AGI’s traces and evals, you can see which span diverges and fix that logic—even if individual outputs vary slightly.
Can I use Future AGI if my agent stack is already built on LangChain or DSPy?
Short Answer: Yes. Future AGI is designed to integrate into existing agent frameworks via SDK-style instrumentation.
Details:
You don’t have to rebuild your agent stack. You instrument your existing LangChain, DSPy, CrewAI, Haystack, or custom orchestration with Future AGI’s SDKs (e.g., traceAI-openai) to capture traces and metrics. From there, you can send your real production interactions into Future AGI as datasets, run experiments comparing different prompts or models, evaluate them with deterministic metrics, and deploy improvements back to your existing agents. The platform slides into your workflow rather than replacing it.
Summary
When the only thing you have is scattered logs, debugging a multi-step, tool-using agent failure feels impossible. The way out is to:
- Reconstruct the failing interaction as a structured trace.
- Turn that trace into a reproducible Dataset scenario.
- Instrument your agent with span-level traces.
- Replay via Experiment, evaluate with deterministic metrics, and localize the failure.
- Use Improve to refine prompts and workflows.
- Continuously Monitor & Protect in production to catch and block similar failures.
That’s how you move from brittle demos to reliable, production-grade agents.
Next Step
Ready to turn scattered logs into a real evaluation and debugging loop for your agents?
Get Started