Skip to navigation
AgentsTesting

RequestRecorder

View as MarkdownOpen in Claude

Tool middleware that records each tool request the agent gets, for CallSimulator.replay(). Add it to the agent with agent.add_tool_middleware(recorder), or to one tool with define_tool(..., middleware=[recorder]). It records the request, then passes the call on unchanged.

Recorded requests hold caller data

The requests carry what the caller said and the call’s data. Keep a recording file where you’d keep call recordings.

from signalwire.testing import RequestRecorder

Parameters

path
str | Path | NoneDefaults to None

A file to append each request to, one JSON object a line. None keeps the requests in memory only.

limit
intDefaults to 1000

Keyword-only. The most requests kept in requests, newest kept. The file isn’t trimmed.

Properties

requests
list[dict[str, Any]]

The requests recorded, oldest first, each with function and argument set.

Methods

load

RequestRecorder.load(path) -> list[dict[str, Any]]

A static method that returns the requests a recorder wrote to path.

path
str | PathRequired

The recording file.

Example

from signalwire import AgentBase, FunctionResult
from signalwire.testing import CallSimulator, RequestRecorder
agent = AgentBase(name="greeter", route="/greeter")
agent.prompt_add_section("Role", "You greet callers.")
def caller_number(args, raw_data):
return FunctionResult(f"The caller is calling from {raw_data.get('caller_id_num')}.")
agent.define_tool("caller_number", "Get the caller's number", {"type": "object", "properties": {}},
caller_number)
recorder = RequestRecorder("tool_requests.jsonl")
agent.add_tool_middleware(recorder)
# Record a request, here from a simulated call
CallSimulator(agent, request_fields={"caller_id_num": "+15551234567"}).call("caller_number")
# Later, run the recorded requests against the agent again
for outcome in CallSimulator(agent).replay(RequestRecorder.load("tool_requests.jsonl")):
print(outcome.name, outcome.response)
# caller_number The caller is calling from +15551234567.