Metadata-Version: 2.4
Name: loopcircuit
Version: 0.1.0
Summary: Autonomous agent loop governor, circuit-breaker, and anti-thrashing engine for AI agents
Author: loopcircuit Contributors
License: MIT
Project-URL: Homepage, https://github.com/VikasPatel22/loopcircuit
Project-URL: Repository, https://github.com/VikasPatel22/loopcircuit.git
Keywords: agent,circuit-breaker,loop-detection,ai-safety,reliability,llm-governance
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# loopcircuit

Autonomous agent loop governor, circuit-breaker, and anti-thrashing engine for AI agents and LLM execution pipelines.

Zero hard dependencies. Framework-agnostic. Runs in sub-millisecond execution time.

---

## The Problem in 2026

Frontier AI agents (Claude Code, Cursor, AutoGen, CrewAI, or custom `while` loops) frequently suffer from **runaway execution traps**:
1. **Exact Repetition**: Invoking the exact same broken tool with identical arguments 20 times in a row.
2. **Ping-Pong Oscillation**: Flip-flopping between two tools (e.g. `edit_file` ➔ `run_test` ➔ `revert` ➔ `edit_file`) without making forward progress.
3. **Error Vortices**: Trapped repeatedly hitting the same underlying exception.
4. **Budget Blowouts**: Burning through $50+ in LLM API credits overnight while unsupervised.

`loopcircuit` provides a deterministic safety net and circuit breaker that halts runaway loops and dynamically injects surgical interventions to snap agents back on track.

---

## Key Capabilities

- **Cycle & Oscillation Detection**: Identifies periodic cycles of length 1 (repetition), length 2 (ping-pong), and length 3+ (wandering) via canonical argument hashing.
- **Error Vortex Recognition**: Normalizes stack traces (stripping memory addresses, line numbers, and timestamps) to detect recurring root causes.
- **Multi-Policy Actions**:
  - `raise`: Immediately trips and raises structured exceptions (`ThrashDetectedError`, `BudgetExceededError`, `MaxStepsExceededError`).
  - `intervene`: Injects a structured system intervention prompt directly into the agent's context to mandate a strategy change.
  - `warn`: Half-opens the circuit, logging telemetry while allowing execution to proceed with probation.
- **Financial & Token Caps**: Hard limits on dollar costs and cumulative token consumption.
- **Drop-in `@governed` Decorator & Context Manager**: Works seamlessly across sync and async loops.

---

## Installation

```bash
pip install loopcircuit
```

---

## Quickstart

### As an Interactive Circuit Breaker

```python
from loopcircuit import CircuitBreaker

# Configure safety thresholds
breaker = CircuitBreaker(
    max_steps=20,
    max_cost_usd=2.00,
    repetition_threshold=3,
    on_trip="intervene"  # Injects corrective prompt instead of crashing
)

for step in agent_execution:
    # Record each tool invocation
    intervention = breaker.record(
        tool_name=step.tool,
        arguments=step.args,
        result=step.output,
        cost_usd=step.cost
    )

    if intervention.is_warning:
        # Feed the generated pattern-breaking prompt back to the LLM
        step.inject_system_prompt(intervention.recovery_prompt)
```

---

### As a Decorator

```python
from loopcircuit import governed

@governed(max_steps=15, max_cost_usd=1.00, on_trip="raise")
def run_agent_loop(task: str, breaker=None):
    while True:
        action = planner(task)
        result = execute(action)
        breaker.record(action.tool, action.args, result=result)
```

---

## CLI Trace Auditor

Replay and audit agent trace logs (`.jsonl`) to detect loops and calculate spend:

```bash
loopcircuit audit agent_run.jsonl
loopcircuit audit agent_run.jsonl --max-cost 2.50 --json
```

---

## License

MIT License.
