Learning / Agentic Operations / Agent Loop & Tool-Call

Agentic Operations · Reference · Level 1 · Foundations · Code

The Agent Loop & Tool-Call Round-Trip

The skeleton every tool-using agent shares. Distilled from the Lesson 0005 build; full working script at ../exercises/m1_agent.py.

The loop in one line: call → observe → decide → repeat — and every iteration rebuilds the model's entire world from the messages list.

The round-trip, step by step

Minimal loop (Python, Anthropic SDK)

messages = [{"role": "user", "content": question}]

for turn in range(1, 11):                      # always bound the loop
    response = client.messages.create(
        model=MODEL, max_tokens=16000,
        system=SYSTEM, tools=TOOLS, messages=messages,
    )
    if response.stop_reason != "tool_use":
        break                                   # model is done

    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            try:
                out, err = run_tool(block.name, block.input), False
            except Exception as e:
                out, err = f"Error: {e}", True  # tell the model, don't crash
            results.append({"type": "tool_result", "tool_use_id": block.id,
                            "content": out, "is_error": err})
    messages.append({"role": "user", "content": results})

Reliability rules (the "reliable" in the M1 gate)

Reading cost off a response

u = response.usage
u.input_tokens, u.output_tokens        # tokens this call
cost = u.input_tokens/1e6 * PRICE_IN + u.output_tokens/1e6 * PRICE_OUT

Note what grows: every loop iteration re-sends the whole history, so input tokens climb each turn — the loop's cost curve is the statelessness lesson made visible.

Tool definitions are prompts

{"name": "calculator",
 "description": "Evaluate one arithmetic expression (...). Call this for
                 every arithmetic step; do not do mental math.",
 "input_schema": {"type": "object",
                  "properties": {"expression": {"type": "string", "description": "..."}},
                  "required": ["expression"]}}

The model chooses tools by reading the description — state when to call it, not just what it does. Official reference: tool use docs.