★ INDIE MAGAZINE · CULTURE · LONG-READS · NO ADS · NO ALGORITHMS ★ INDIE MAGAZINE · CULTURE · LONG-READS · NO ADS · NO ALGORITHMS ★ INDIE MAGAZINE · CULTURE · LONG-READS · NO ADS · NO ALGORITHMS ★ INDIE MAGAZINE · CULTURE · LONG-READS · NO ADS · NO ALGORITHMS ★

How to Reproduce a Tool-Calling Bug Before Changing the Model

This practical guide explains why you must reproduce a tool-calling bug by freezing and replaying the exact request before changing models or prompts. Most failures stem from application-level issues like validation errors or missing message appends. By following a structured debugging playbook, developers can isolate root causes and fix application defects instead of chasing phantom model errors.

How to Reproduce a Tool-Calling Bug Before Changing the Model

The short answer: when a tool-calling flow misbehaves, freeze the exact request that produced the failure, replay it with the same model, the same tools, and the same message history, and capture the full response before you touch the prompt or switch models. Most tool-calling bugs are application-level (validation, message ordering, tool-result handling, or logging gaps). Changing the model first usually adds variables instead of removing them.

I have lost multiple evenings swapping models and rewriting tool descriptions only to discover that the real bug was a missing tool_call_id, a swallowed validation error, or a code path that never executed the function. The habit that stopped the waste is simple: reproduce first, change second. This article is the reproduction playbook I now follow.

What This Solves

You have a tool-calling failure—wrong arguments, function never ran, final answer ignored the tool result, or the loop never terminated—and you want to fix the actual cause. By the end of this post you will know:

  • Exactly what to capture so the failure can be replayed

  • A minimal reproduction script shape

  • The order of investigation that keeps the model constant

  • When it is finally rational to change the model or the tool description

This belongs in Debugging, Testing, and Field Notes. Reproduction is the difference between diagnosis and superstition.

Who Should Use This Approach

Developers who already have a tool-calling or small-agent path in production or staging and have watched it fail in a way that is not immediately obvious. If you have not yet implemented a complete request-to-execution example, start with that article. Reproduction is most powerful when the happy path already works.

The Reproduction Principle

A bug you cannot replay is a bug you cannot confidently fix.
A bug you can replay with the model held constant is almost always an application bug.

Keep the model, the tool definitions, and the message history fixed until the failure is under your control. Only after the failure is reproducible should you consider whether the model’s behavior is the part that needs to change.

What to Capture at the Moment of Failure

A documentary photo of a laptop screen displaying API request history, message logs, and debugging data on a desk.

When the bug appears, record:

  1. Exact model string used for every turn

  2. Full tool definitions (the JSON you sent)

  3. Complete message list at the point of failure, including every assistant tool-call message and every tool-result message

  4. Raw arguments the model produced for the failing tool call

  5. finish_reason and usage for the relevant turns

  6. Application logs that show whether validation ran, whether the real function was entered, and what tool result was returned

  7. SDK / client version and base URL

  8. Timestamp and environment (staging, production, local)

If any of these is missing, your first job is to add the logging that would have captured it. Do not start changing the prompt yet.

Minimal Reproduction Script

Turn the captured data into a short, self-contained script:

Python

import json
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
)

MODEL = "grok-4-1-fast-reasoning"  # the exact string from the failure
TOOLS = [ /* paste the exact tool definitions */ ]

# Paste the exact message list that preceded the failure
messages = [
    {"role": "system", "content": "..."},
    {"role": "user", "content": "..."},
    # ... include every assistant and tool message up to the failure point
]

print("=== REPLAY REQUEST ===")
completion = client.chat.completions.create(
    model=MODEL,
    messages=messages,
    tools=TOOLS,
    tool_choice="auto",
)

choice = completion.choices[0]
print("finish_reason:", choice.finish_reason)
print("content:", choice.message.content)
print("tool_calls:", choice.message.tool_calls)
print("usage:", completion.usage)

# If the failure was in your handler, also replay the handler in isolation
if choice.message.tool_calls:
    for tc in choice.message.tool_calls:
        print("\n=== HANDLER REPLAY ===")
        print("name:", tc.function.name)
        print("raw_args:", tc.function.arguments)
        # call your real handle_tool_call here and print the result

Run the script. If it reproduces the bad behavior, you now own the failure. If it does not, the missing ingredient is usually an earlier message, a different tool definition, or an environment-specific configuration.

Investigation Order (Model Stays Fixed)

  1. Does the replay produce the same tool call and the same arguments?
    If yes, the model is consistent; the bug is downstream in your code.

  2. Did validation run?
    Check the logs for a validate step. If it is missing, that is the first fix.

  3. Was the real function entered?
    A log line inside the function body is the only reliable proof. If it is absent, the handler returned early or took a wrong branch.

  4. Was the tool-result message appended with the correct tool_call_id?
    Message-ordering and id-mismatch bugs are extremely common and look like “the model ignored the tool.”

  5. Did the subsequent model turn receive the updated messages?
    Print the message list immediately before the next create call.

  6. Only after the above are clean: consider whether the tool description or the system prompt is misleading the model into producing unusable arguments.

This order prevents the classic waste of “upgrading the model” when the application never executed the function in the first place.

Common Bugs That Look Like Model Problems

  • Function never ran because validation failed silently

  • Tool result sent with the wrong or missing tool_call_id

  • Assistant tool-call message never appended

  • Handler caught an exception and returned an empty success string

  • Code treated the first tool-call response as the final answer

  • Max-turns or timeout logic exited without recording why

All of these survive model changes. All of them disappear once the reproduction script and the handler logs are examined.

When It Is Rational to Change the Model or the Prompt

After the application path is proven correct and the same unusable arguments keep appearing:

  • Tighten the tool description and parameter descriptions

  • Add a short, explicit example of valid arguments in the system prompt

  • Narrow the schema (stricter ranges, fewer optional fields)

  • As a last step, evaluate whether a different model follows the schema more reliably on this particular tool

Even then, keep the reproduction script. It becomes the regression test that tells you whether the prompt or model change actually helped.

A Note from the Home Lab

A nightly agent started producing empty search results. The model was “requesting the tool correctly” according to the high-level logs. I almost switched models. Instead I froze the message list and replayed it. The raw arguments contained a limit of 0. My validator was supposed to reject it; a recent refactor had commented out the range check. The real search function was never called; an empty list was returned as a successful tool result. Restoring the validation fixed the issue in minutes. The model had been consistent the entire time. The application had changed.

That episode is why I now refuse to change the model until the reproduction script exists and the handler evidence is in hand.

A documentary photo of a small home lab server blinking with status LED lights in a tech workspace.

Practical Checklist

  • Exact model, tools, and message history from the failure are captured

  • A short replay script reproduces the bad behavior

  • Logs show whether validation ran and whether the real function was entered

  • Tool-result messages carry the correct tool_call_id

  • The investigation order keeps the model fixed until the application path is cleared

  • Any prompt or model change is verified against the same reproduction script

When a Simpler Approach Is Better

If the failure is a one-off and the cost of a wrong answer is low, a quick manual inspection may be enough. As soon as the same class of failure can recur, invest in the reproduction script. The script is cheaper than the third late-night model comparison.

Closing

Tool-calling bugs feel like model problems because the model is the most visible component. Most of the time the break is in the application’s handling of the tool call. Freeze the request, replay it, and read the evidence before you change the model or the prompt.

Reproduction turns a mysterious failure into a concrete, fixable defect.

Build the replay.
Explain every field you needed to capture.
Verify that the same failure appears under your control before you change anything else.

Build it. Explain it. Verify it.

Next we will look at managing xAI SDK versions without breaking your application—so the next client upgrade does not become another source of hard-to-reproduce failures.

Comments

No comments yet — be the first to share a thought.

Leave a comment