Terminal-native agent resilience middleware for Python.
AgentLatch is a zero-dependency framework that makes AI agents resilient and observable. It solves two massive pain points in agent development:
-
Silent Tool Failures — When an LLM executes a tool that crashes, AgentLatch intercepts the Python exception, prevents a runtime crash, and feeds a structured JSON error back to the LLM so it can self-correct.
-
Blind Latency — It tracks millisecond execution time of LLM vs. tools and prints a color-coded ASCII flamegraph directly in the terminal using the
richlibrary. No API keys, no dashboards, no cloud.
Quick Install
uv pip install -e ".[dev]"
Usage
from agentlatch import profile_agent, safe_tool
@safe_tool
def query_database(sql: str) -> str:
"""This tool is now protected — exceptions become JSON errors."""
import sqlite3
conn = sqlite3.connect("my.db")
return str(conn.execute(sql).fetchall())
@safe_tool(timeout=5.0)
def call_api(url: str) -> str:
"""This tool has a 5-second timeout."""
import requests
return requests.get(url).text
@profile_agent
def run_agent():
"""The agent loop — traced and visualized automatically."""
result = query_database("SELECT * FROM users")
weather = call_api("https://api.weather.com/sf")
return f"Got {result} and {weather}"
run_agent()
What Happens
- Execution: Every
@safe_toolcall is timed and protected. - On Error: Instead of crashing, the tool returns a JSON error string:
{ "status": "error", "error_type": "ProgrammingError", "message": "column 'age' does not exist", "instruction": "The tool execution failed. Review your parameters and retry with corrected inputs." }
- On Completion: A rich flamegraph is printed to the terminal:
┌─────────────────────────────────────────────────────────┐ │ ⚡ AGENTLATCH EXECUTION PROFILE │ │ Total: 1.23s │ Tools: 0.85s │ LLM Reasoning: 0.38s │ ├─────────────────────────────────────────────────────────┤ │ ████████████████████████████████████████████ 1.23s │ │ ░░░░████████████░░░████░░░░░░░░░░░░░░░░░░░░ │ │ query_db 0.5s call_api 0.2s │ │ ▲ ERROR │ │ Legend: █ LLM █ Tool (OK) █ Tool (ERROR) │ └─────────────────────────────────────────────────────────┘
Features
| Feature | Description |
|---|---|
@safe_tool |
Wraps any function — catches exceptions, returns JSON errors |
@safe_tool(timeout=N) |
Adds a thread-based timeout (cross-platform) |
@profile_agent |
Traces the full agent loop and renders the flamegraph |
| Async support | Both decorators work with async def functions |
| Framework agnostic | Works with LangGraph, AutoGen, CrewAI, or vanilla scripts |
| CI-safe | Banner auto-disables in non-TTY environments |
Running Examples
# Vanilla agent with a forced failure + self-correction
python examples/vanilla_agent.py
# LangGraph-style state machine
python examples/langgraph_agent.py
Running Tests
uv pip install -e ".[dev]"
pytest tests/ -v
Architecture
contextvars— Thread-safe trace propagation without manual trace IDsconcurrent.futures— Cross-platform timeouts (nosignal.alarm)rich— Premium terminal rendering (the only external dependency)
License
MIT
Metadata
Release files for agentlatch 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| agentlatch-0.1.0.tar.gz | 24.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agentlatch-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.9 kB
Release files / agentlatch-0.1.0.tar.gz
| Download URL | agentlatch-0.1.0.tar.gz |
|---|---|
| Size | 24.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbbd1163607deaefe8bfd69fffbb97bee621e8009915b05be8d1c22f0381310c
|
|
BLAKE2b-256 checksum How to use checksums |
757f4de4190f527446d46dc5735af3322b6ee0f968d9f1202afc988c5ef9ebad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / agentlatch-0.1.0-py3-none-any.whl
| Download URL | agentlatch-0.1.0-py3-none-any.whl |
|---|---|
| Size | 19.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c23c4a178620272e1b511b657372cde5f49e08539a023dad9c581a5a6fd65e74
|
|
BLAKE2b-256 checksum How to use checksums |
46f9c794248295dcaeb5093f7aa1e243536e907b8f322ad71ddca6fb79829b2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for agentlatch 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.