Skip to main content

Documentation Index

Fetch the complete documentation index at: https://reagent-ai.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Installation

uv add reagent-flow-anthropic

Setup

Wrap your Anthropic client with patch():
from anthropic import Anthropic
from reagent_flow_anthropic import patch
import reagent_flow

client = patch(Anthropic())
patch() wraps client.messages.create to automatically log tool_use content blocks into the active reagent-flow session.

Basic usage

with reagent_flow.session("chat") as s:
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        messages=[{"role": "user", "content": "Look up order 123"}],
        tools=[...],
        max_tokens=1024,
    )

s.assert_called("lookup_order")

Tool result capture

When you thread tool results back as user messages with tool_result content blocks, the adapter captures them automatically:
with reagent_flow.session("chat") as s:
    # Turn 1: model requests tool execution via tool_use block
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        messages=[{"role": "user", "content": "Look up order 123"}],
        tools=[...],
        max_tokens=1024,
    )

    # User code runs the tool
    tool_use_block = response.content[0]  # type: tool_use
    tool_result = execute_tool(tool_use_block)

    # Turn 2: send the result back — adapter captures it automatically
    client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Look up order 123"},
            {"role": "assistant", "content": response.content},
            {
                "role": "user",
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": tool_use_block.id,
                        "content": json.dumps(tool_result),
                    }
                ],
            },
        ],
    )

# Tool output contract works end-to-end
s.assert_tool_output_matches("lookup_order", schema={
    "status": str,
    "amount": float,
})
The adapter handles both string content and list-of-text-blocks content in tool_result blocks. JSON-encoded content is automatically decoded for schema validation.

What gets captured

DataSourceLogged as
Tool callstool_use content blockslog_llm_call(tool_calls=...)
Response texttext content blockslog_llm_call(response_text=...)
Model nameresponse.modellog_llm_call(model=...)
Token usageresponse.usage (input_tokens, output_tokens)log_llm_call(token_usage=...)
Tool resultstool_result blocks in next call’s user messageslog_tool_result(...)

Streaming

Streaming (stream=True) is detected and skipped with a warning. Use stream=False for traced calls.

No session active

If no reagent-flow session is active when create() is called, the adapter is a no-op.