Skip to main content

A minimal agent loop for tool-using language models

Project description

simple_agent_loop

A minimal agent loop for tool-using language models. ~250 lines. Handles message format conversion, parallel tool execution, session compaction, and subagent composition.

Install

pip install simple-agent-loop

Setup

import anthropic
import json
import simple_agent_loop as sal

client = anthropic.Anthropic()  # uses ANTHROPIC_API_KEY env var

def invoke_model(tools, session):
    kwargs = dict(
        model="claude-sonnet-4-5",
        max_tokens=16000,
        messages=session["messages"],
    )
    if "system" in session:
        kwargs["system"] = session["system"]
    if tools:
        kwargs["tools"] = tools
    return client.messages.create(**kwargs).to_dict()

Hello World

No tools, single turn -- the model just responds:

session = init_session(
    system_prompt="You are a helpful assistant.",
    user_prompt="Say hello in three languages.",
)
result = agent_loop(invoke_model, [], session, max_iterations=1)
print(response(result)["content"])

Tool-Using Agent

Define tools as Anthropic tool schemas and provide handler functions. The handler receives tool input as keyword arguments and returns a string.

import requests

tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"},
            },
            "required": ["city"],
        },
    }
]

def get_weather(city):
    resp = requests.get(f"https://wttr.in/{city}?format=j1")
    data = resp.json()["current_condition"][0]
    return json.dumps({
        "city": city,
        "temp_c": data["temp_C"],
        "description": data["weatherDesc"][0]["value"],
    })

session = init_session(
    system_prompt="You answer weather questions. Use the get_weather tool.",
    user_prompt="What's the weather in Tokyo and Paris?",
)
result = agent_loop(
    invoke_model, tools, session,
    tool_handlers={"get_weather": get_weather},
)
print(response(result)["content"])

The model will call get_weather twice (in parallel), see the results, and respond with a summary. The loop runs until the model responds without making any tool calls.

Subagents

A subagent is a tool handler that runs its own agent loop. The outer agent calls it like any tool and gets back a string result.

Example: Text Compressor (compressor.py)

A coordinator agent iteratively compresses text using two subagents: a shortener and a quality judge.

# Subagent: compresses text
def shorten(text):
    session = init_session(
        system_prompt="Rewrite the text to half its length. Output ONLY the result.",
        user_prompt=text,
    )
    result = agent_loop(invoke_model, [], session, name="shortener", max_iterations=1)
    shortened = response(result)["content"]
    ratio = len(shortened) / len(text)
    return json.dumps({"compression_ratio": round(ratio, 3), "shortened_text": shortened})

# Subagent: judges compression quality
def judge(original, shortened):
    session = init_session(
        system_prompt=(
            "Compare original and shortened text. Return ONLY JSON: "
            '{"verdict": "acceptable", "reason": "..."} or '
            '{"verdict": "too_lossy", "reason": "..."}'
        ),
        user_prompt=f"ORIGINAL:\n{original}\n\nSHORTENED:\n{shortened}",
    )
    result = agent_loop(invoke_model, [], session, name="judge", max_iterations=1)
    return response(result)["content"]

The coordinator has tools for shorten and judge, and its system prompt tells it to loop: shorten, judge, stop if too_lossy or diminishing returns, otherwise shorten again. Each subagent is a one-shot agent loop (max_iterations=1) with no tools of its own.

Example: Transform Rule Derivation (derive_transform.py)

A more complex example with four subagents and a coordinator. Given a source text and target text, it derives general transformation rules and specific info that together reproduce the target from the source.

# Subagent: applies rules + specific info to source text
def edit(text, rules, specific_info):
    session = init_session(
        system_prompt="Apply the rules to the text using the specific info. Output ONLY the result.",
        user_prompt=f"SOURCE TEXT:\n{text}\n\nRULES:\n{rules}\n\nSPECIFIC INFO:\n{specific_info}",
    )
    result = agent_loop(invoke_model, [], session, name="editor", max_iterations=1)
    return response(result)["content"]

# Subagent: scores how close the output is to the target
def judge_similarity(editor_output, target):
    session = init_session(
        system_prompt='Compare texts. Return JSON: {"score": 0-100, "differences": "..."}',
        user_prompt=f"EDITOR OUTPUT:\n{editor_output}\n\nTARGET:\n{target}",
    )
    result = agent_loop(invoke_model, [], session, name="similarity-judge", max_iterations=1)
    return response(result)["content"]

# Subagent: checks rules are abstract (no specific content leaked in)
def judge_generality(rules):
    ...

# Subagent: checks specific_info is a flat fact list
def judge_specific_info(specific_info):
    ...

The coordinator calls edit, then calls all three judges in parallel, refines based on scores, and repeats until all judges score above 90. Tool calls within a single model response execute in parallel automatically.

API Reference

Session Management

  • init_session(system_prompt, user_prompt) - Create a new session
  • extend_session(session, message) - Append a message to the session
  • send(session, user_message) - Add a user message to the session
  • fork_session(session) - Deep copy a session for branching
  • response(session) - Get the last assistant message, or None

Agent Loop

  • agent_loop(invoke_model, tools, session, tool_handlers=None, name=None, max_iterations=None)
    • invoke_model(tools, session) - Function that calls the model API
    • tools - List of Anthropic tool schemas ([] for no tools)
    • session - Session dict from init_session
    • tool_handlers - Dict mapping tool names to handler functions
    • name - Agent name for log output
    • max_iterations - Max model calls before stopping (None = unlimited)
    • Returns the session with all messages appended

Message Format

Messages use a generic format independent of any API:

{"role": "system", "content": "..."}
{"role": "user", "content": "..."}
{"role": "assistant", "content": "..."}
{"type": "thinking", "content": "..."}
{"type": "tool_call", "name": "...", "id": "...", "input": {...}}
{"type": "tool_result", "id": "...", "output": "..."}

Conversion to/from Anthropic API format is handled by to_api_messages() and parse_response().

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_agent_loop-0.1.1.tar.gz (39.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_agent_loop-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file simple_agent_loop-0.1.1.tar.gz.

File metadata

  • Download URL: simple_agent_loop-0.1.1.tar.gz
  • Upload date:
  • Size: 39.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.12

File hashes

Hashes for simple_agent_loop-0.1.1.tar.gz
Algorithm Hash digest
SHA256 19a9deb43f93a01cf2a30acf1b9667ba8b0274b4582ee248b4d6860f470796c1
MD5 3435b342b8541fdd79b22f0c87a0c742
BLAKE2b-256 e89f115633c1a120cd557f277ad88ed5c0591ccc221ea136d4e3f1d9aac78bcc

See more details on using hashes here.

File details

Details for the file simple_agent_loop-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_agent_loop-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 124c8c832f0cde2e2462687a118589c80dc21080c8ecd9dc0e2f8d680e47076f
MD5 4760520433329dad664c1b6360e9eaea
BLAKE2b-256 5a6cb8e3ce1340fba112ebbdde14325b9b67dad3eb9a2f40fb0e7842760f087d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page