explaingit

justvugg/agentmw

4PythonAudience · developerComplexity · 4/5ActiveLicenseSetup · moderate

TLDR

Python middleware that wraps any LLM client and watches agent sessions for loops, redundant tool calls, contradictions, abandonment, and hallucinations.

Mindmap

mindmap
  root((agentmw))
    Inputs
      Agent conversation turns
      Tool call results
      Saved trace files
    Outputs
      Failure classifications
      Compressed context
      Reasoning patterns DB
      Time travel reports
    Use Cases
      Detect agent loops live
      Compress stale tool results
      Recall past success patterns
      Replay failed sessions
    Tech Stack
      Python
      SQLite
      ONNX
      OpenAI
      Anthropic

Things people build with this

USE CASE 1

Catch agent loops and redundant tool calls before they burn tokens

USE CASE 2

Compress long agent traces by dropping stale tool result blocks

USE CASE 3

Build a memory of past agent patterns recalled by semantic search

USE CASE 4

Replay a failed session and ask the model to simulate a fork point

Tech stack

PythonSQLiteONNXOpenAIAnthropicOllama

Getting it running

Difficulty · moderate Time to first run · 30min

pip install is simple but useful features need an LLM provider key and the ONNX embedding extra.

Apache 2.0 lets you use, modify, and distribute the code commercially as long as you keep the license and notice files.

In plain English

agentmw is a piece of middleware for AI agents. When developers build programs that let a language model take many turns of action (for example, calling tools, reading files, then answering), the model can get stuck in loops, repeat the same tool call, contradict itself, give up halfway, or make up facts. agentmw sits between your code and the model, watches what is happening in each session, and tries to catch these failures while the run is still going. It is open source, released under Apache-2.0, and the README states it works with any model and any framework. The library has several layers. An LLM monitor sends recent turns to a provider (Ollama, OpenAI, Anthropic, or OpenRouter) to classify them as loop, redundant tool call, contradiction, abandonment, or hallucination. A heuristic monitor runs the same checks with regex rules as a prefilter and as a fallback. A compression layer trims stale tool-result blocks from the conversation while keeping the most recent ones. A reasoning library in SQLite remembers patterns from past runs and recalls them by meaning using a small local embedding model (BGE-small ONNX, 30 MB). There is also a time-travel command-line tool that walks through a saved trace, identifies the "point of no return" where the agent went off-track, counts how many tokens were wasted, and can ask the provider to simulate what would have happened on the other branch. After each session, a background extractor distills one to three reusable patterns and adds them to the reasoning library, so the system grows its own memory of what worked and what did not. A circuit breaker short-circuits provider calls after three failures in 30 seconds, with a 60-second cooldown, so a sick monitor never slows down the main client. Installation is via pip, with optional extras for semantic recall and for the MCP server (a protocol for talking to tools like Claude Desktop and Cursor). The agentmw command-line tool includes a demo, a config viewer, memory save and recall, session listing, timeline and replay, an extract command, and a stats command. From Python, you wrap a normal client like anthropic.Anthropic with the wrap function and call it as usual. After the call, a trace object exposes which monitors triggered, the compression ratio, and which past patterns were recalled. Config reads defaults, then a TOML file, then environment variables, then explicit arguments.

Copy-paste prompts

Prompt 1
Wrap an existing Anthropic-based agent with agentmw and log every loop detection to OpenTelemetry
Prompt 2
Add a new failure category to agentmw for tool-call argument drift and write the heuristic regex prefilter
Prompt 3
Build a FastAPI dashboard on top of agentmw's SQLite reasoning library to browse recalled patterns
Prompt 4
Configure agentmw via TOML to use Ollama for the monitor calls and OpenAI for the main agent
Prompt 5
Write a CI script that runs agentmw replay on saved traces and fails the build if any session shows abandonment
Open on GitHub → Explain another repo

Generated 2026-05-22 · Model: sonnet-4-6 · Verify against the repo before relying on details.