{"slug": "show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents", "title": "Show HN: AgentState – Open-source resilience and caching proxy for AI agents", "summary": "AgentState, an open-source resilience and caching proxy for AI agents, automatically checkpoints execution state in SQLite, handles retries, and enables pause, edit, and resume of runs from any point, saving tokens and time. In benchmark tests simulating complex 50-step autonomous agent runs with forced crashes, AgentState achieved crash recovery in ~0.015 seconds (7,360x faster than standard agents) and 100% token cost savings on retries via cache hits.", "body_md": "**Stop wasting tokens when AI agents crash.** When an agent fails on step 87 out of 100, you typically lose the entire execution history and have to restart from step 0.\n\n**AgentState** is a lightweight, self-hosted proxy that intercepts your LLM and tool calls, automatically checkpoints their execution state in SQLite, handles retries, and lets you pause, edit, and resume runs from any point—saving you money and time.\n\n``` python\nfrom agentstate import AgentStateOpenAI\n\n# Automatically routes all completions through AgentState proxy!\nclient = AgentStateOpenAI(session_id=\"session_user_9812\", step_number=0)\n\nresponse = client.chat.completions.create(\n    model=\"gpt-4o\",\n    messages=[{\"role\": \"user\", \"content\": \"Analyze system performance metrics.\"}]\n)\npython\nfrom langchain_openai import ChatOpenAI\nfrom agentstate import wrap_langchain\n\nllm = ChatOpenAI(**wrap_langchain(session_id=\"session_user_9812\"))\npython\nfrom crewai import Agent\nfrom agentstate import wrap_crewai\n\nagent = Agent(\n    role=\"Research Analyst\",\n    goal=\"Analyze market data\",\n    llm_config=wrap_crewai(session_id=\"session_user_9812\")\n)\n```\n\nNo SDKs required. Just point your LLM client's `baseURL`\n\nto the AgentState proxy:\n\n``` python\nfrom openai import OpenAI\n\nclient = OpenAI(\n    api_key=\"your-api-key\",\n    base_url=\"http://localhost:8080/v1\", # <-- Route through AgentState\n    default_headers={\"x-agent-session-id\": \"session_user_9812\", \"x-agent-step-number\": \"0\"}\n)\n```\n\n**🔌 1-Line Integration:** Native`AgentStateOpenAI`\n\nwrapper or simple`baseURL`\n\nswapping for OpenAI, LangChain, and CrewAI.**💾 Automatic Checkpointing:** Every prompt, response, and tool invocation is saved to a local SQLite database.**⚡ Instant Cache Recovery:** Replay steps in**~15ms ($0.00 token cost)** on retries.**✋ Human-in-the-Loop Gateway:** Intercept sensitive tool calls (`send_email`\n\n,`execute_command`\n\n,`stripe_charge`\n\n) and pause execution until approved via dashboard or API.**🔀 Multi-Model Fallback:** Transparently reroutes requests to fallback models (e.g.`gpt-3.5-turbo`\n\n, Claude, Ollama) if the primary provider hits rate limits (429) or server errors (500).**📥 Fine-Tuning Dataset Exporter:** Export production agent trajectories as OpenAI-compatible`.jsonl`\n\nfine-tuning datasets in a single click.**🔔 Webhook Alerts:** Real-time Slack/Discord webhooks when agent runs fail or require human approval.**🎛️ Session Replay & Rollback:** Visual dashboard to inspect agent trajectories. Rollback to any step and resume execution cleanly.\n\nIn benchmark tests simulating complex 50-step autonomous agent runs with forced crashes:\n\n| Metric | Standard Agent (No Proxy) | AgentState Proxy | Improvement |\n|---|---|---|---|\nCrash Recovery Time (Step #45) |\n~110.4 seconds | ~0.015 seconds |\n7,360x faster 🚀 |\nToken Cost on Retry |\n$2.45 per failed retry | $0.00 (100% Cache Hit) |\n100% Savings 💰 |\nResilience to Consecutive Failures |\nCrashes run after 1 error | Recovered from 5+ consecutive crashes |\n100% Execution Completion |\nRogue Action Risk |\nUnmonitored | 100% Intercepted by HITL Gateway |\nZero Unapproved Actions |\n\n``` php\ngraph TD\n    A[AI Agent / Application] -->|1. LLM / Tool Request| B[AgentState Proxy]\n    B -->|2. Checkpoint State| C[(SQLite Database)]\n    B -->|3. Forward Request| D[OpenAI / Claude / Local LLM]\n    D -->|4. Return Response| B\n    B -->|5. Update Cache & Log| C\n    B -->|6. Return to Agent| A\ngit clone https://github.com/aleenz1102/AgentState.git\ncd agentstate\n\npython -m venv venv\n# On Windows (PowerShell):\n.\\venv\\Scripts\\activate\n# On macOS/Linux:\nsource venv/bin/activate\n\npip install fastapi uvicorn httpx openai playwright Pillow imageio\npython server.py\n```\n\n- Proxy endpoint:\n`http://localhost:8080/v1`\n\n- Embedded Dashboard:\n`http://localhost:8080/dashboard`\n\n-\n**Resilience & Caching Demo:**\n\n```\npython test_agent.py\n```\n\n*(Simulates an agent crash on Step #2, then recovers instantly on retry via cache)* -\n**Human-in-the-Loop Gateway Demo:**\n\n```\npython test_hitl_agent.py\n```\n\n*(Pauses terminal agent execution when a sensitive action is attempted until approved on dashboard)* -\n**Comprehensive Feature Suite:**\n\n```\npython test_full_suite.py\n```\n\n*(Tests 1-Line wrappers, fallback models, dataset exporter, and webhooks)*\n\n: OpenAI-compatible completion proxy.`POST /v1/chat/completions`\n\n**Headers:**`x-agent-session-id`\n\n(Required): Unique session tracking ID.`x-agent-step-number`\n\n(Optional): Step index of execution loop.`x-agent-require-approval`\n\n(Optional): Trigger HITL approval gateway.`x-agent-fallback-model`\n\n(Optional): Reroute model if primary provider fails.\n\n: List all logged sessions.`GET /api/sessions`\n\n: Get session details and step history.`GET /api/sessions/{session_id}`\n\n: Rollback session to specified step.`POST /api/sessions/{session_id}/reset`\n\n: List pending human approvals.`GET /api/approvals/pending`\n\n: Approve or reject pending action (`POST /api/approvals/{id}/action`\n\n`{\"action\": \"APPROVED\" | \"REJECTED\"}`\n\n).: Download bulk fine-tuning`GET /api/export/dataset`\n\n`.jsonl`\n\ndataset.\n\nWe welcome contributions! Please see our [CONTRIBUTING.md](/aleenz1102/AgentState/blob/main/CONTRIBUTING.md) for setup instructions and pull request guidelines.\n\nAgentState is designed for inclusion in the following AI ecosystem resources:\n\nAgentState is open-source software licensed under the [MIT License](/aleenz1102/AgentState/blob/main/LICENSE).", "url": "https://wpnews.pro/news/show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents", "canonical_source": "https://github.com/aleenz1102/AgentState", "published_at": "2026-07-25 09:30:09+00:00", "updated_at": "2026-07-25 09:52:23.833959+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "ai-infrastructure"], "entities": ["AgentState", "OpenAI", "LangChain", "CrewAI", "SQLite", "Claude", "Ollama"], "alternates": {"html": "https://wpnews.pro/news/show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents", "markdown": "https://wpnews.pro/news/show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents.md", "text": "https://wpnews.pro/news/show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents.txt", "jsonld": "https://wpnews.pro/news/show-hn-agentstate-open-source-resilience-and-caching-proxy-for-ai-agents.jsonld"}}