How to Test an AI Agent's Tool Calls with pytest (Without Paying for API Calls) A developer published a hands-on guide showing how to test an AI agent's tool calls with pytest using a scripted FakeLLM that replays canned responses, eliminating real LLM calls and API costs. The approach records every tool call in a trace and uses Pydantic argument models to verify tool selection and argument validity, with five tests running in under a second. Chatbots say things. AI agents do things: they check the weather, book meetings, update tickets. So a bug is no longer just a bad sentence. It's a meeting booked on the wrong day, or an API hit in an endless loop. The good news: most of that risk lives in your code around the model , and plain pytest can test it. In this guide you'll build a tiny agent and write 5 tests that run in under a second, with no real LLM and no API cost . You need Python 3.10+ and pip install pytest pydantic . An agent runs in a loop: the model picks a tool, your code runs it, the result goes back to the model, repeat until it answers. Four things break most often: "next friday" instead of a date We'll write one check for each. python agent.py import json from dataclasses import dataclass, field @dataclass class ToolCall: name: str arguments: dict @dataclass class LLMResponse: content: str | None = None tool calls: list ToolCall = field default factory=list class MaxStepsExceeded Exception : pass class Agent: def init self, llm, tools, max steps=5 : self.llm, self.tools, self.max steps = llm, tools, max steps self.trace = every tool call the agent made def run self, user message : messages = {"role": "user", "content": user message} for in range self.max steps : response = self.llm.chat messages if not response.tool calls: return response.content for call in response.tool calls: self.trace.append call try: result = self.tools