{"slug": "show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes", "title": "Show HN: Agentfork – Replay an LLM agent from any step and test fixes", "summary": "A developer released agentfork, a local CLI tool that records every LLM and tool call in an agent run, replays earlier steps from cache, and sweeps many variants of a single step to produce pass rates instead of single-run anecdotes. In the tool's refund-agent example, a $49.99 order read from an API in cents (\"amount\": 4999) was refunded as $4,999 by the original prompt and by gpt-4o-mini and gpt-4o across 3 repeats each (0/3 pass), while adding one system-prompt sentence to divide cents by 100 fixed it at 3/3 pass for about $0.0001 per run. The tool installs via `pip install \"agentfork-cli[openai]\"` (or `agentfork-cli[anthropic]`), stores runs in `.agentfork/traces.db`, and supports LangGraph agents through `agentfork.wrap_langchain`; streaming models and the OpenAI Responses API are not yet recorded.", "body_md": "**Fork your LLM agent at any step, then sweep prompts, models or tool results to find what actually fixes it.**\n\nWhen an agent misbehaves at step 3, the usual loop is: tweak the prompt, re-run everything, eyeball one output, repeat. That is slow, costs tokens on steps that didn't change, and one lucky run proves nothing. agentfork records every LLM and tool call, replays earlier steps from cache, and runs **many variants of one step, several times each**, so you get pass rates instead of anecdotes.\n\nEverything runs locally. No account, no server, no data leaves your machine.\n\nA refund agent reads `\"amount\": 4999, \"currency\": \"USD\"` from an order API. The amount is in cents, and the agent refunds **$4,999** for a $49.99 laptop ([examples/refund_agent_cents.py](https://github.com/bimal1023/agentfork/blob/main/examples/refund_agent_cents.py)). Is the fix a better model or a better prompt?\n\n``` bash\n$ agentfork sweep 543da612 --step 3 --repeat 3 --expect \"49.99\" --model gpt-4o-mini,gpt-4o\nEstimated cost: about $0.0050\n\n  variant             ok    pass   tokens   time      cost   sample output\n  original            1/1   0/1       446   1.7s   $0.0001   I have processed a refund of $4,999.00...\n  model=gpt-4o-mini   3/3   0/3       446   1.4s   $0.0001   I have processed a refund of $4,999.00...\n  model=gpt-4o        3/3   0/3       394   1.2s   $0.0015   The full amount of $4,999.00 for order A123...\n```\n\nA bigger model costs 15x more per run and doesn't fix it. One sentence in the prompt does:\n\n``` bash\n$ agentfork sweep 543da612 --step 3 --repeat 3 --expect \"49.99\" \\\n    --system \"You are a support agent. Look up the order, then refund the full order amount in USD.\" \\\n    --system \"... Order amounts from the API are in cents; divide by 100 before refunding.\"\nEstimated cost: about $0.0006\n\n  variant     ok    pass   tokens   time      cost   sample output\n  original    1/1   0/1       446   1.7s   $0.0001   I have processed a refund of $4,999.00...\n  system #1   3/3   0/3       445   1.2s   $0.0001   I've successfully refunded you $4,999.00...\n  system #2   3/3   3/3       457   1.2s   $0.0001   I have processed a refund of $49.99...\n```\n\nSteps 1 and 2 replayed from cache in every run, so only the step that mattered was paid for, and the runs went 4 at a time.\n\n```\npip install \"agentfork-cli[openai]\"      # or agentfork-cli[anthropic]\n```\n\nAdd three things to your agent:\n\n``` python\nfrom openai import OpenAI\nfrom agentfork import trace, tool, wrap_openai\n\nclient = wrap_openai(OpenAI())      # records every LLM call (wrap_anthropic for Claude)\n\n@tool                               # records every tool call\ndef lookup_order(order_id): ...\n\n@trace                              # one run = one call of your agent\ndef run_agent(question): ...\n```\n\nRun your agent as usual. Each run is saved to `.agentfork/traces.db`.\n\nWrap the chat model and put `@agentfork.tool` under LangChain's `@tool`. Forks and sweeps then work on the graph like on any other agent ([examples/langgraph_refund_agent.py](https://github.com/bimal1023/agentfork/blob/main/examples/langgraph_refund_agent.py)):\n\n``` python\nimport agentfork\nfrom langchain_core.tools import tool\nfrom langchain_openai import ChatOpenAI\nfrom langgraph.prebuilt import create_react_agent\n\nllm = agentfork.wrap_langchain(ChatOpenAI(model=\"gpt-4o-mini\"))   # or ChatAnthropic\n\n@tool\n@agentfork.tool\ndef lookup_order(order_id: str) -> str:\n    \"\"\"Look up an order by id.\"\"\"\n    ...\n\ngraph = create_react_agent(llm, [lookup_order])\n\n@agentfork.trace\ndef run_agent(question):\n    return graph.invoke({\"messages\": [(\"user\", question)]})[\"messages\"][-1].content\n```\n\nStreaming models (`streaming=True`) and the OpenAI Responses API aren't recorded yet.\n\n| Command | What it does | \n|---|---|\n| `agentfork list` | Recent runs with steps, tokens, duration and fork lineage | \n| `agentfork show <id> [--full]` | A run as a step tree, with optional full JSON | \n| `agentfork fork <id> --step N --output/--model/--system ...` | Re-run from step N with one change; earlier steps come from cache | \n| `agentfork sweep <id> --step N --model a,b --system ... --repeat K --expect TEXT [--parallel P]` | Try many variants of step N, K times each, P at a time (default 4), and compare pass rate, tokens, time and cost | \n| `agentfork diff <a> <b>` | Compare two runs step by step | \n| `agentfork save-test <id>` | Save a run as a pytest test that replays from cache in CI with no API key | \n| `agentfork ui [--port N]` | Open the local web UI to browse, fork, sweep and compare runs | \n\nPrefer clicking to typing? The same fork, sweep and diff are available in a local web UI.\n\n```\npip install \"agentfork-cli[openai,ui]\"\nagentfork ui        # opens http://127.0.0.1:8000\n```\n\nStart it from the folder you ran your agent in, since runs are saved to `.agentfork/` there. The server only listens on your own machine.\n\n1. \n**Find the step that went wrong.** Pick a run on the left. Its timeline shows each LLM and tool call in plain words, such as`Decided to call issue_refund(amount_usd=4999)` . Click a step to see its full input and output.\n2. \n**Try a change.** On that step, use another model, edit the system prompt, or fake a tool's result. Earlier steps replay from cache, and the new run appears under the original.\n3. \n**Test several.** Enter a few changes, how many times to run each, and text the final output must contain. You get a pass rate per change, with the original run as the baseline:\n\n```\nChange      Passed   Example output\nOriginal    ○ 0/1    I have processed a refund of $4,999.00...\nChange 1    ●●● 3/3  I have processed a refund of $49.99...\nChange 2    ○○○ 0/3  I have processed a refund of $4,999.00...\n```\n\n4. \n**Compare.** Tick two runs to see them side by side, with the first step where they differ highlighted.\n\n1. **Record.**`@trace` ,`@tool` and`wrap_openai` /`wrap_anthropic` /`wrap_langchain` capture each step's inputs, outputs, tokens and latency, plus a hash of its inputs.\n2. **Fork.** agentfork re-imports your agent and re-runs it. Steps before N are served from the cache, step N gets your change, and everything after runs live. A model change applies to every LLM call from step N on.\n3. **Sweep.** Runs fork for each variant`--repeat` times,`--parallel` at a time, and checks each final output for your`--expect` text. Before it starts, it estimates the cost from the tokens the original run used from step N on, priced per model.\n4. **Test.**`save-test` writes a JSON snapshot plus a pytest file. The test replays every step from cache and fails with a diff if any step's inputs, the step count, or the final output changes.\n\nCosts use list prices per model for OpenAI and Anthropic, built into [pricing.py](https://github.com/bimal1023/agentfork/blob/main/src/agentfork/pricing.py). Prices change, and a model without a price shows `-`. To add or correct one, put USD per 1M input and output tokens in `.agentfork/prices.json`:\n\n```\n{\"gpt-4o-mini\": [0.15, 0.60], \"my-finetune\": [3.0, 12.0]}\n```\n\nagentfork overlaps with record/replay tools like [agent-rewind](https://pypi.org/project/agent-rewind/), which supports many more providers, and [agentstep](https://pypi.org/project/agentstep/), a LangGraph time-travel debugger with a web UI. agentfork's focus is the step after replay: sweeping many variants of one step, with repeats and pass rates, to choose a fix with evidence.\n\n- Recorder, CLI viewer, fork, diff, snapshot tests\n- OpenAI and Anthropic support\n- Sweep with repeats and pass rates\n- Local web UI\n- Cost estimates per variant\n- Parallel sweeps\n- LangGraph integration\n\nMIT", "url": "https://wpnews.pro/news/show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes", "canonical_source": "https://github.com/bimal1023/agentfork", "published_at": "2026-10-17 07:53:40+00:00", "updated_at": "2026-10-17 08:00:15.797125+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "large-language-models", "ai-safety"], "entities": ["agentfork", "OpenAI", "gpt-4o", "gpt-4o-mini", "LangChain", "LangGraph", "Anthropic"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes", "markdown": "https://wpnews.pro/news/show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes.md", "text": "https://wpnews.pro/news/show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes.txt", "jsonld": "https://wpnews.pro/news/show-hn-agentfork-replay-an-llm-agent-from-any-step-and-test-fixes.jsonld"}}