Skip to main content

loop-agent: an embeddable loop engine for agents (gather -> act -> verify -> repeat, with composable stop conditions, persisted state, limited human gates, and an outer Reflexion loop).

Project description

loop-agent

PyPI Python CI License: MIT

loop-agent is a small Python runtime for practicing Loop Engineering. Inside an agent or an existing application, it runs the following process:

  1. Use gather to retrieve the next task to work on
  2. Use act to execute that task
  3. Use review to accept or reject the artifact before verification
  4. Use verify to validate the result against ground truth
  5. If successful, end the loop; if not yet complete, continue to the next iteration
  6. Stop when a limit is reached, such as the maximum number of iterations, time, budget, or stagnation

What matters in Loop Engineering is not having a person instruct an agent one step at a time, but designing the loop: what to gather, how to execute it, how to verify it, and when to stop. loop-agent is the engine for running that loop. It lets users focus on the events inside the loop: what to work on, how to work on it, how to verify that it is complete, and under what conditions to stop when things are not going well.

A defining feature is that loops can be expressed as Python functions or through the CLI. You can have coding agents such as Claude Code or Codex implement and run loops for you. The loops they write remain as Python code, so you can inspect the code and deepen your understanding.

What It Is For

Use loop-agent when you want to safely repeat processes like the following. The result of each iteration is kept in history, and the final result is returned as something like "succeeded", "stopped at a limit", or "stopped pending approval".

  • Process accumulated GitHub issues
  • Have a coding agent keep fixing code until tests pass
  • Process multiple files one by one, recording each completed item
  • Run an external CLI or model call, then move to the next attempt if it fails
  • Persist long-running work in state.db and resume it after interruption
  • Require manual approval only for irreversible operations such as commit / push

Installation

pip install loop-agent

If you want coding agents such as Claude Code / Codex / Cursor to write loops for you, also install the skill for loop-agent.

loop-agent install-skills
loop-agent install-skills --target-agent codex
loop-agent install-skills --target-agent cursor

Minimal Example

from loop_agent import ActOutcome, MaxIterations, ReviewOutcome, VerifyOutcome, run_loop

n = {"value": 0}

def act(_ctx):
    n["value"] += 1
    return ActOutcome(observation=f"step {n['value']}")

def verify(_outcome):
    return VerifyOutcome(goal_met=n["value"] >= 3)

def review(_outcome):
    return ReviewOutcome(approved=True, feedback="small local loop")

result = run_loop(
    act=act,
    review=review,
    verify=verify,
    conditions=[MaxIterations(5)],
)

print(result.status, result.reason)

When verify returns goal_met=True, the loop stops as a success. Even if it does not succeed, stop conditions such as MaxIterations ensure that the loop will stop.

Loop Components

A loop-agent loop is mainly composed of six elements.

Name Role
gather Selects the next target to execute
act Performs the actual work
review Accepts or blocks the artifact before verification
verify Checks whether the work succeeded
conditions Stops based on count, time, budget, stagnation, and similar limits
gate Inserts manual approval only for operations that need it

If you omit gather, the current state is passed directly to act. review is required by the API; use skip_review("reason") only when artifact review is intentionally unnecessary, such as a tiny deterministic local loop.

Create a Loop Template

You can generate a loop template from the CLI.

loop-agent init-harness --template light  --output ./harness-light
loop-agent init-harness --template claude --output ./harness-claude
loop-agent init-harness --template codex  --output ./harness-codex

This generates a short harness.py and README. After generation, edit the prompt, review policy, verification command, stop conditions, and targets for manual approval to fit your use case. Generated harnesses include a required review stub that blocks by default; set SKIP_REVIEW=True with a reason only when review is intentionally unnecessary. The Claude and Codex templates include resumable state, JSONL progress events, and a repeated-failure cutoff so long runs can be observed and resumed.

Reflexion is intentionally not in the starter harness. Start by making the inner loop observable and verifiable. Add Reflexion after the run shows a repeated, lesson-shaped failure; adding it before the prompt, verifier, and review are clear can turn noisy first attempts into bad stored lessons.

Using It with Coding Agents

loop-agent is also designed for workflows where coding agents such as Claude Code, Codex, or Cursor write loops from prompts like this:

Using loop-agent, write a loop that fixes failing pytest tests.
For act, delegate the fixes to a coding agent, and for verify, judge success by the pytest exit code.
Stop after at most 5 attempts, and keep commit and push outside the loop.

Installing the skill makes it easier for coding agents to find loop-agent APIs and design patterns.

Main Features

  • Synchronous / asynchronous loop execution: run_loop, async_run_loop
  • Stop conditions: maximum iterations, time, tokens, stagnation detection, and more
  • Required artifact review with an explicit skip helper
  • Verification helpers: CommandVerifier, PytestVerifier, RegexVerifier
  • State recording and resume: progress file / state.db
  • Manual approval: pause / resume only for irreversible operations
  • Adapters: ClaudeCodeAct, CodexAct
  • Processing multiple targets: WorkListGather
  • Observation and operations: summary, dashboard, spike scan
  • Outer improvement loop: Reflexion for repeated, lesson-shaped failures

Fit Criteria

loop-agent is well suited to work whose completion can be judged mechanically, such as by tests or command results.

Good examples:

  • pytest passes
  • Only specific files have changed
  • A command exits with code 0
  • String or AST conditions are satisfied
  • All N tasks become done

Poor fits:

  • "Make the writing better"
  • "Generally improve the quality"
  • Work where success judgment depends on human intuition every time

You can still use loop-agent for ambiguous goals, but in that case, designing verify becomes the central concern.

Documentation

Document Contents
docs/quickstart.md Run your first loop
docs/first-harness-api.md APIs to use first
docs/seams.md Details of gather / act / review / verify and related components
docs/verifiers.md Verification helpers
docs/recipes/ Concrete loop examples
docs/adapters/README.md Claude Code / Codex adapters
docs/persistence-and-resume.md State persistence and resume
docs/safety.md Stop conditions and manual approval
docs/cli.md CLI
docs/stability.md Compatibility contract
docs/api-reference.md API reference

Status

1.0.0 Stable. The canonical compatibility contract is docs/stability.md.

This README is kept short as an entry point, with detailed specifications split into docs.

License / Development

The license is MIT.

Issues / PRs are handled in English. The default branch is main.

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

loop_agent-1.1.0.tar.gz (693.5 kB view details)

Uploaded Source

Built Distribution

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

loop_agent-1.1.0-py3-none-any.whl (280.5 kB view details)

Uploaded Python 3

File details

Details for the file loop_agent-1.1.0.tar.gz.

File metadata

  • Download URL: loop_agent-1.1.0.tar.gz
  • Upload date:
  • Size: 693.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for loop_agent-1.1.0.tar.gz
Algorithm Hash digest
SHA256 26e1e20d12ecc2169d1b017e219062a0de6bbd003b20eff69596b87db378c993
MD5 2603ae52723eaa73c582fad132db1566
BLAKE2b-256 3dcbfebbc880d26a748de990c2cb2710f397b71529bbff4720be29f7e928bc46

See more details on using hashes here.

Provenance

The following attestation bundles were made for loop_agent-1.1.0.tar.gz:

Publisher: release.yml on happy-ryo/loop-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file loop_agent-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: loop_agent-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 280.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for loop_agent-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25db8477c66dcb04a77d4fdf20693c305b001a436e8cd7b89f1a901088d91529
MD5 e33736f7d5a4ee45a94d94b3858145fd
BLAKE2b-256 e3bb80037ae5c91f7a1031d9009bba59b95c93c23a333970d30ebd791be55e77

See more details on using hashes here.

Provenance

The following attestation bundles were made for loop_agent-1.1.0-py3-none-any.whl:

Publisher: release.yml on happy-ryo/loop-agent

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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