Skip to main content

agent-runloop

pip install agent-runloop, then import agent_loop.

An explicit agent loop for tool-using LLM agents, and blocks to compose agents into routers, waterfalls, parallel fan-outs, handoffs and review loops.

The model never calls anything. On every turn it returns one JSON decision: "call this tool with these arguments" or "here is the final answer". The rest is ordinary code:

generate -> parse JSON -> validate decision -> look up tool -> validate arguments
  -> reject duplicates -> check budget, precondition, authorization
  -> execute (timeout) -> feed result back as delimited, untrusted data -> generate ...

Every rejection (bad JSON, unknown tool, invalid arguments, duplicate call, tool exception) becomes an observation the model sees on its next turn. None of them execute anything.

pip install agent-runloop         # core: pydantic only
pip install "agent-runloop[trace]"   # + flow-xray call-tree tracing

Python 3.11+.

What the loop does

  • Validated tool calls. Tool arguments are pydantic models. Invalid calls go back to the model as structured errors, never to your code.
  • Human approval for writes. A tool with writes=True never runs on the model's word. The run pauses as awaiting_approval with the proposed call. resume_agent(run_id, approved=True|False) continues it, from another request or another process. The approve step is a compare-and-set, so two approvals cannot both execute.
  • Budgets. Hard limits per run on model turns, executed tool calls, tokens actually used and wall time. They carry across pause and resume.
  • Crash recovery. The loop saves a checkpoint to SQLite with a lease before every model turn. If the process dies, recover_agent(run_id) continues from the last checkpoint once the lease expires.
  • Authorization and preconditions. These come from trusted run context, never from model output. They are checked when a call is proposed (a human is never asked to approve a forbidden action) and again at execution.
  • Idempotency. current_call_id gives each tool call a stable id (run id + hash of the validated call). A write tool can use it as an idempotency key.
  • Prompt-injection hygiene. Tool output goes back delimited and labelled as data, with the original task restated after it. The system message starts with a fixed instruction-hierarchy policy.
  • Audit and versions. One audit row per tool execution. Every result records the model and a hash of the whole system prompt (instructions, protocol, tool catalog).
  • Live events. Pass an asyncio.Queue to stream steps, for example over SSE.

Quick start

import asyncio
from pydantic import BaseModel
from agent_loop import Budget, SqliteAgentRunStore, ToolSpec, run_agent
from agent_loop.ollama import OllamaBackend

class SearchArgs(BaseModel):
    query: str

async def search(args: SearchArgs, trace_id: str):
    return [{"doc_id": "refunds", "text": "Refunds take 5 to 7 business days."}]

result = asyncio.run(run_agent(
    "How long does a refund take?",
    "You are a support agent. Answer only from search results.",
    [ToolSpec("search_knowledge_base", "Search the knowledge base.", SearchArgs, search)],
    backend=OllamaBackend("llama3.2:3b"),
    run_store=SqliteAgentRunStore("runs.sqlite3"),
    budget=Budget(max_iterations=4, max_tool_calls=4, max_tokens=8000, max_seconds=60),
))
print(result.status, result.answer, result.usage)

result.status is one of answered, awaiting_approval, max_iterations, budget_exceeded, timeout, rejected_final, model_unavailable or cancelled. result.steps lists every step, including rejected ones.

Human approval

TICKET = ToolSpec("create_ticket", "Open a support ticket.", TicketArgs, create_ticket, writes=True,
                  authorize=lambda ctx: ctx.get("role") == "customer")

r = await run_agent(task, instructions, [SEARCH, TICKET], backend=b, run_store=store, context={"role": "customer"})
if r.status == "awaiting_approval":
    print(r.pending_call)                       # {"tool": "create_ticket", "arguments": {...}}
    r = await resume_agent(r.run_id, True, [SEARCH, TICKET], store, backend=b)

Other hooks

  • final_check(answer, citations, steps) -> reason | None accepts or rejects the final answer, for example "every citation must be a doc_id a tool returned".
  • prefetch=[("search_knowledge_base", {"query": task})] runs read tools before the first model turn.
  • ToolSpec(precondition=...) is a deterministic business rule checked on a proposed call.

Composing agents

Every block is a step, async (task, ctx) -> Outcome, so blocks nest freely.

Block What it does
Agent One run of the loop: instructions, tools, model, budget, final check.
Sequence Waterfall: each step's answer becomes part of the next task. Stops at the first step that does not answer.
Router A classifier picks one route. The classifier is any function (keyword rules, embeddings) or LLMClassifier, which constrains the model to the route names.
Parallel Branches run concurrently. Results are merged with join_answers, majority_vote, your own function, or an aggregator agent.
Handoff The source agent gets a handoff_to_<target> tool. The target sees only the structured note, not the conversation. At most one hop.
Refine Evaluator-optimizer: the worker answers, a critic reviews, the worker revises, up to max_rounds. The critic is a rule function or LLMCritic.
Fallback Tries steps in order; the first that answers wins.
Timeout A wall-time limit around any step.
step(fn) Any async (task, ctx) -> str | Outcome function as a step.
from agent_loop import (Agent, Context, Fallback, Handoff, LLMClassifier, LLMCritic, Parallel, Refine, Router,
                        Sequence, Timeout, majority_vote)

billing   = Agent("billing", "You are the billing agent. Answer only from search results.", [SEARCH])
technical = Agent("technical", "You are technical support. Answer only from search results.", [SEARCH])
writer    = Agent("writer", "Rewrite the previous result as one polite sentence.")

pipeline = Timeout(
    Router(
        {"billing": Sequence(billing, writer),                       # waterfall inside a route
         "technical": Refine(technical, LLMCritic("Must give concrete steps."), max_rounds=2)},
        LLMClassifier({"billing": "payments, refunds, plans", "technical": "bugs and errors"}),
        default="technical",
    ),
    seconds=120,
)

ctx = Context(backend=OllamaBackend("llama3.2:3b"), run_store=SqliteAgentRunStore("runs.sqlite3"),
              context={"subject": "user-42"})
outcome = await pipeline("When will my refund arrive?", ctx)
print(outcome.status, outcome.answer, outcome.data["route"], len(outcome.runs))

Outcome has status, answer, runs (every AgentResult inside, in order) and data, which records what each block decided: the route, the votes, review rounds, which fallback answered.

examples/patterns.py runs every block against a local model and captures each run as a flow-xray trace.

Limits

  • When an agent inside a composite pauses for approval, the composite stops and returns awaiting_approval. Resuming that agent run does not continue the rest of the composite.
  • Token and tool budgets apply per agent run. Use Timeout for a deadline across a whole composite.
  • majority_vote compares exact answers, so use it for labels and numbers, not free text.
  • The run store is SQLite: one host, or a shared file system with working file locks.
  • Recovering after a crash during an approved write re-executes that write. It is safe only if the tool is idempotent across processes (current_call_id).

Backends

The loop needs any object with:

async def generate(self, messages, format=None, options=None, trace_id=None) -> str

format is a JSON schema the reply must follow. A backend should report tokens with agent_loop.usage.add(prompt, completion), or max_tokens is not enforced. Two are included.

OpenAI-compatible APIs (pip install "agent-runloop[openai]"): OpenAI, Azure OpenAI, and servers that speak the same protocol (vLLM, LM Studio, OpenRouter, llama.cpp server, Ollama's /v1).

from agent_loop.openai import AzureOpenAIBackend, OpenAIBackend

OpenAIBackend("gpt-4.1-mini")                                    # OPENAI_API_KEY
OpenAIBackend("llama3.2:3b", base_url="http://127.0.0.1:11434/v1", api_key="unused")
AzureOpenAIBackend("my-deployment", endpoint="https://<resource>.openai.azure.com")  # AZURE_OPENAI_API_KEY
AzureOpenAIBackend("my-deployment", endpoint=..., token_provider=get_token)        # Entra ID, refreshed per call
  • Retries. Only what can succeed on a second try is retried: connection errors, timeouts, 429 and 5xx. The wait honours Retry-After and retry-after-ms, otherwise it is exponential backoff with jitter. Any other 4xx fails at once with ModelRequestError.
  • Circuit breaker. After breaker_threshold consecutive failed calls, every call fails fast with ModelUnavailable for breaker_reset_s. Then one trial call decides whether it closes. The breaker is per backend instance and per process.
  • Structured output. The loop's schema is sent as a json_schema response format. Use structured="json_object" for servers that only support JSON mode, or "none".
  • Tokens. Usage from each response feeds the run's token budget and the flow-xray trace.

Inside a run, an unavailable model ends the run as model_unavailable with its state saved. It does not crash the loop.

Ollama (agent_loop.ollama.OllamaBackend) is a dependency-free reference backend with no retries.

Observability

  • flow-xray: with agent-runloop[trace] installed, run_agent, each tool execution, each composite block and each model call become nodes in a flow-xray call tree, with inputs, outputs, tokens and latency.
  • Step events: by default they go to the agent_loop logger at DEBUG. To send them to your own logging and trace-id binding:
from agent_loop import hooks
hooks.configure(log=my_log_step, bind_trace=my_bind_trace)   # log(trace_id, step, **fields)

License

MIT

Async SQLite persistence (0.3.0)

run_agent, resume_agent and recover_agent await the store's asave, aload, acheckpoint, aclaim_for_resume, aclaim_orphaned, and aaudit operations. Use await acancel_agent(run_id, store) from async callers; cancel_agent and every synchronous store method remain available for sync code. Store construction initializes the schema synchronously: construct before serving requests, or use await run_sqlite(SqliteAgentRunStore, path) during async startup.

agent_loop.sqlite_io.run_sqlite(callable, *args, **kwargs) runs a whole operation on a separate two-thread SQLite pool. Open, commit/rollback and close connections inside that callable; never pass a connection or cursor across threads. Its admission limit is two submitted operations per event loop. Trace, usage and idempotency context variables are copied into the worker. Each call is traced with kind=sqlite and its operation name when the optional trace integration is installed.

Cancellation (including repeated cancellation while waiting for capacity) waits for the operation to finish and close its connection, then propagates. A database error takes precedence over cancellation. A completed terminal save is not overwritten with cancelled; a completed tool observation is recorded before awaiting its audit. Cancellation during a resume claim can leave its existing lease to expire, without executing the approved tool. No lease, schema or approval CAS policy changes here. Drain request tasks before stopping the event loop. Forced loop teardown, process crashes and SIGKILL are outside this completion guarantee; SQLite rollback/recovery and existing run leases still apply. This does not guarantee every node is under 50 ms or remove synchronous CPU work.

Verification: python -m unittest discover -s tests -v. Tests use real SQLite transactions and held write locks, including queued/repeated cancellation, write failure, worker connection lifetime, cancellation during checkpoint/final save/audit, and two competing approvals. These are runtime regressions, not a universal refinement proof of the durable run protocol. Existing sync APIs remain the matched baseline; the SQL statements and serialized state schema are unchanged.

Metadata

Release files for agent-runloop 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-runloop 0.3.0
File Size Uploaded Provenance Actions
agent_runloop-0.3.0.tar.gz 33.1 kB With provenance Details for agent_runloop-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-runloop 0.3.0
File Interpreter ABI Platform Actions
agent_runloop-0.3.0-py3-none-any.whl Python 3 none any Details for agent_runloop-0.3.0-py3-none-any.whl

Total release size: 69.9 kB

Release files / agent_runloop-0.3.0.tar.gz

Download URL agent_runloop-0.3.0.tar.gz
Size 33.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c838d89a1804955ccc4ff6a0ef988fb38afb2836082618c7e03eae5ac4c4cf9f
BLAKE2b-256 checksum
How to use checksums
83f6d739eeebb9d67f07a3ec165d25bc8312f92275f2ce82aa905cf86b6da91d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.

Transparency log

Release files / agent_runloop-0.3.0-py3-none-any.whl

Download URL agent_runloop-0.3.0-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5c0a2857e4ae34c82e984454a4441234cde2b28942659c594be9da480f67e22
BLAKE2b-256 checksum
How to use checksums
f9875bea2e8595f640ebd8d2ea75ee8ac6ce44dc3927c64338916ed96176146a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.

Transparency log

Security for agent-runloop 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for agent_runloop-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page