Testing Tool-Calling Workflows

Ma Mahalakshmi V Updated 19 Sep 2026
7 min read ·Lesson 168 of 224

Separating What the Model Decides From What Your Code Does

A tool-calling (function-calling) workflow has two distinct halves. The model decides which tool to call and with what arguments — that decision is a model-behavior question, and whether the model tends to make good decisions belongs to the evaluation layer (Unit 13). Your code then takes whatever the model decided, looks up the matching local function, executes it, and feeds the result back — that dispatch-and-execution logic is ordinary, deterministic Python code, and it is exactly the kind of thing this unit's testing techniques apply to directly.

This lesson focuses entirely on the second half: given a tool call the model already produced (simulated with a fake response, never a real one), does your routing code behave correctly? This distinction matters enough to repeat, because it is the single most common confusion when testing agentic code: a test that asserts "the model should have called get_weather" is an eval; a test that asserts "given a tool call for get_weather, my dispatcher invokes the get_weather Python function with the right arguments" is a unit test.

A Minimal Tool-Calling Dispatcher

import json


def get_weather(city: str) -> str:
    fake_data = {"Paris": "18C, cloudy", "Tokyo": "24C, clear"}
    return fake_data.get(city, "unknown city")


def get_exchange_rate(base: str, quote: str) -> str:
    fake_rates = {("USD", "EUR"): 0.92, ("USD", "JPY"): 149.3}
    rate = fake_rates.get((base, quote))
    return f"{rate}" if rate is not None else "rate unavailable"


TOOL_REGISTRY = {
    "get_weather": get_weather,
    "get_exchange_rate": get_exchange_rate,
}


def dispatch_tool_call(tool_name: str, arguments_json: str) -> str:
    if tool_name not in TOOL_REGISTRY:
        raise ValueError(f"Unknown tool requested: {tool_name!r}")

    try:
        arguments = json.loads(arguments_json)
    except json.JSONDecodeError as exc:
        raise ValueError(f"Malformed arguments for {tool_name}: {exc}") from exc

    function = TOOL_REGISTRY[tool_name]
    return function(**arguments)

dispatch_tool_call is the exact seam this lesson tests: it accepts a tool name and a raw JSON argument string (matching the shape the SDK gives you for a function-call output item), looks the tool up in a registry, parses the arguments, and invokes the corresponding Python function. Every branch here is deterministic and fully within your control, which is precisely why it deserves direct, thorough unit test coverage rather than being folded into a broader evaluation.

Testing the Routing Logic With Simulated Tool Calls

def test_dispatch_tool_call_routes_to_get_weather():
    result = dispatch_tool_call("get_weather", '{"city": "Paris"}')
    assert result == "18C, cloudy"
    print("PASS: dispatch_tool_call routes get_weather correctly")


def test_dispatch_tool_call_routes_to_get_exchange_rate():
    result = dispatch_tool_call("get_exchange_rate", '{"base": "USD", "quote": "JPY"}')
    assert result == "149.3"
    print("PASS: dispatch_tool_call routes get_exchange_rate correctly")


def test_dispatch_tool_call_rejects_unknown_tool():
    try:
        dispatch_tool_call("get_stock_price", '{"ticker": "ACME"}')
        raised = False
    except ValueError:
        raised = True
    assert raised
    print("PASS: dispatch_tool_call rejects an unregistered tool name")


def test_dispatch_tool_call_rejects_malformed_json():
    try:
        dispatch_tool_call("get_weather", '{"city": "Paris"')  # missing closing brace
        raised = False
    except ValueError:
        raised = True
    assert raised
    print("PASS: dispatch_tool_call rejects malformed argument JSON")

Each test simulates exactly one scenario the model's output could produce — a known tool with valid arguments, an unrecognized tool name, and malformed JSON — without ever calling the model to produce it. This is the crucial technique: you are constructing the model's hypothetical output by hand, as a plain string, the same way you constructed fake API responses in earlier lessons. The malformed-JSON case matters especially in real systems, because while well-behaved models rarely emit invalid JSON for a well-specified tool schema, "rarely" is not "never," and code that crashes with an unhandled JSONDecodeError in production is a code-quality bug, not a model-quality one.

Testing the Full Tool-Calling Loop With a Fake Client

A more complete workflow involves a loop: call the model, check whether it requested a tool call, execute the tool, send the result back, and repeat until the model produces a final answer. Testing this loop requires a fake client capable of returning different responses on successive calls — a step up in sophistication from the single-response fakes used earlier.

class ScriptedFakeClient:
    """Returns a pre-scripted sequence of responses, one per call."""

    def __init__(self, scripted_responses: list):
        self._responses = list(scripted_responses)
        self.call_count = 0

        class _Responses:
            def create(inner_self, **kwargs):
                response = self._responses[self.call_count]
                self.call_count += 1
                return response

        self.responses = _Responses()


class FakeToolCallResponse:
    def __init__(self, tool_name: str, arguments_json: str):
        self.tool_calls = [{"name": tool_name, "arguments": arguments_json}]
        self.output_text = None


class FakeFinalResponse:
    def __init__(self, text: str):
        self.tool_calls = []
        self.output_text = text


def run_tool_calling_loop(client, user_input: str, max_turns: int = 5) -> str:
    for _ in range(max_turns):
        response = client.responses.create(model="gpt-5.6-terra", input=user_input)
        if not response.tool_calls:
            return response.output_text

        call = response.tool_calls[0]
        tool_result = dispatch_tool_call(call["name"], call["arguments"])
        user_input = f"Tool result: {tool_result}"

    raise RuntimeError("Exceeded max_turns without a final answer")


def test_run_tool_calling_loop_executes_one_tool_then_finishes():
    fake_client = ScriptedFakeClient([
        FakeToolCallResponse("get_weather", '{"city": "Tokyo"}'),
        FakeFinalResponse("It is 24C and clear in Tokyo."),
    ])

    result = run_tool_calling_loop(fake_client, "What's the weather in Tokyo?")

    assert result == "It is 24C and clear in Tokyo."
    assert fake_client.call_count == 2
    print("PASS: run_tool_calling_loop executes a tool call then returns the final answer")


def test_run_tool_calling_loop_raises_after_max_turns():
    endless_tool_call = FakeToolCallResponse("get_weather", '{"city": "Paris"}')
    fake_client = ScriptedFakeClient([endless_tool_call] * 5)

    try:
        run_tool_calling_loop(fake_client, "Weather?", max_turns=5)
        raised = False
    except RuntimeError:
        raised = True

    assert raised
    print("PASS: run_tool_calling_loop raises RuntimeError instead of looping forever")

ScriptedFakeClient holds a list of pre-built responses and returns the next one in sequence on each call, tracked with call_count — this simulates a multi-turn conversation without any real model involved, and it does so completely deterministically, so the test produces the exact same result every time it runs. The first test confirms the ordinary path: a tool call followed by a final answer, in exactly two round trips. The second test confirms an important safety property — that a model which keeps requesting tools indefinitely does not turn into an infinite loop in your application, but instead fails loudly with RuntimeError. This kind of guard, and the test that proves it works, is easy to omit and expensive to omit: an infinite loop against a real API in production silently burns cost with every iteration.

Testing That the Tool Result Is Correctly Fed Back

A subtle bug in tool-calling loops is constructing the follow-up message incorrectly — for example, forgetting to include the tool's result, or attaching it to the wrong tool-call ID in a multi-tool-call turn. This is worth testing explicitly by inspecting what was actually sent on the second call.

def test_run_tool_calling_loop_feeds_tool_result_back_into_next_call():
    fake_client = ScriptedFakeClient([
        FakeToolCallResponse("get_weather", '{"city": "Paris"}'),
        FakeFinalResponse("Done."),
    ])

    run_tool_calling_loop(fake_client, "Weather in Paris?")

    # Reconstruct what the second call actually received by re-invoking manually,
    # since ScriptedFakeClient does not store per-call inputs by default here.
    assert fake_client.call_count == 2
    print("PASS: the loop made exactly two calls, implying the tool result was used")

In a real test suite you would typically extend ScriptedFakeClient to also record each call's kwargs (the same last_kwargs pattern from Lesson 2), then assert that the second call's input contains the tool's actual result string — this confirms the feedback step is wired correctly rather than, for instance, silently discarding the tool's output and re-sending the original question unchanged.

Common Mistakes

  • Testing whether the model chose the right tool, in a unit test. That is an evaluation concern requiring realistic inputs and a grading rubric (Unit 13); a unit test should assume a specific tool call happened and verify only that your code handles it correctly.
  • Not testing the "unknown tool" and "malformed arguments" branches. These are the exact cases most likely to appear the first time the model behaves unexpectedly in production, and they are also the cheapest to test, since they require no realistic model behavior at all — only a hand-built string.
  • Building a tool-calling loop test that never sets a turn limit and calling it "safe" without testing the limit. Without a specific test for the max-turns path, an infinite-loop bug can go undetected until it appears as a runaway API bill.

Best Practices

  • Draw a hard line between "does the model pick the right tool" (eval) and "does my dispatcher route correctly given a tool call" (unit test), and keep each concern in its own test suite.
  • Simulate multi-step conversations with a scripted fake client that returns a fixed sequence of responses, so multi-turn tool-calling loops are testable without any real model calls.
  • Explicitly test failure and boundary paths — unknown tool names, malformed arguments, and the maximum-turns safeguard — since these are the paths most likely to cause real production incidents and the easiest to verify with fakes.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Testing Tool-Calling Workflows and get answers drawn from it.

Signed-in readers only.