<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pygg</title>
    <description>The latest articles on DEV Community by pygg (@pygg).</description>
    <link>https://dev.to/pygg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4266760%2F2169702b-9eda-482b-94ea-310642b01857.png</url>
      <title>DEV Community: pygg</title>
      <link>https://dev.to/pygg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pygg"/>
    <language>en</language>
    <item>
      <title>My coding agent kept dying mid-task. So I built a local gateway that routes around dead providers</title>
      <dc:creator>pygg</dc:creator>
      <pubDate>Thu, 26 Nov 2026 11:26:41 +0000</pubDate>
      <link>https://dev.to/pygg/my-coding-agent-kept-dying-mid-task-so-i-built-a-local-gateway-that-routes-around-dead-providers-44oa</link>
      <guid>https://dev.to/pygg/my-coding-agent-kept-dying-mid-task-so-i-built-a-local-gateway-that-routes-around-dead-providers-44oa</guid>
      <description>&lt;h1&gt;
  
  
  My coding agent kept dying mid-task. So I built a local gateway that routes around dead providers
&lt;/h1&gt;

&lt;p&gt;Halfway through a refactor, Claude Code stopped. Not with a useful error — with a rate limit. The plan had hit its limit, the session was stuck, and switching models by hand meant starting the conversation over. I did that a few times too many, and then I built Pygg.&lt;/p&gt;

&lt;p&gt;Pygg is a local gateway for Claude Code, Codex, OpenCode and anything that speaks OpenAI- or Anthropic-format APIs. It runs on your own machine, and it exists for one job: keep your coding agent moving when a provider says no.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;You point your coding tool at the gateway and pick the model &lt;code&gt;auto&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install -g @pygg/cli@dev
pygg gateway add-connection --preset gemini --key-env GEMINI_API_KEY
pygg setup claude
pygg run claude
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gateway listens on &lt;code&gt;127.0.0.1:8741&lt;/code&gt;. Each request goes to an eligible model you connected, within the limits you set. When a provider answers with a rate limit, an exhausted quota, no credit or an overload, the next eligible model in the route takes the request — with the whole conversation, tool calls and all — before your agent sees an error. The session stays on that model while it can serve.&lt;/p&gt;

&lt;p&gt;Three things I cared about while building it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The fallback must not lose context.&lt;/strong&gt; A session stays on one key, so the provider's prompt cache keeps working. For OpenAI-format agents talking to Claude, Pygg turns on Anthropic's prompt cache automatically. In 20 staged limit events across Chat Completions, Messages and Responses sessions, 20 resumed on the fallback with the whole conversation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cost must be traceable, not vibes.&lt;/strong&gt; Every attempt lands in a ledger with what it cost and where that figure came from: actual (the provider said so), estimated (a price you or a catalog set) or unknown — never shown as $0. Spending limits hold per key, per session or per day, and a strict limit holds what each request could cost at most, so concurrent requests can't pass it together. When a tool started by &lt;code&gt;pygg run&lt;/code&gt; exits, you get a receipt: what the launch cost, how much came from cache, which models answered, and what changed in the repo.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A failed provider must not mean a double-billed request.&lt;/strong&gt; A timeout, a dropped connection or a 5xx that isn't an overload stops instead of trying elsewhere, because the provider may already have run it. Each attempt is written to disk before it is sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Delegating to cheaper agents — with checks, not hope
&lt;/h2&gt;

&lt;p&gt;The second half is Smart Tasks, still experimental. You hand a well-defined change to a cheaper agent over MCP (&lt;code&gt;pygg_run_task&lt;/code&gt;), and it runs in a Git worktree, cheapest first. Your checkout isn't touched until you keep the result as a branch — and nothing is kept until your repository's own checks pass. A result no check ran on is marked &lt;em&gt;not ready&lt;/em&gt; instead of looking done. In my runs, 50 results with no check run were all refused as not ready, and step caps held under 10 × 1,000 adversarial requests with zero violations.&lt;/p&gt;

&lt;p&gt;Cheap-first isn't just theory. On six hard cases with half their tests hidden from the agent, cheap-first solved 16 of 16 recorded runs at about a fifth of the strong model's list cost per solved run.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it actually saved
&lt;/h2&gt;

&lt;p&gt;These are my own runs, with method and raw results in the repo — read them before quoting a number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every connected model, Nov 20:&lt;/strong&gt; 36 fixture tasks, 15 routes, one attempt each. &lt;code&gt;auto/cheap&lt;/code&gt; solved 36/36 on plan quota; the best metered route, Gemini 4 Hydrogen, solved 36/36 at $0.072 per solved task; &lt;code&gt;auto/code&lt;/code&gt; on Gemini 4 Carbon solved 33/36 at $0.115. The two local 8B models solved 3/36 and 0/36.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The 17 hardest tasks, three times each, Nov 20:&lt;/strong&gt; DeepSeek V4.1 Flash solved 51/51; Gemini 4 Hydrogen 50/51 at $0.106 per solved run; &lt;code&gt;auto/cheap&lt;/code&gt; 49/51 on plan quota. Mid-run, Gemini's prepaid credit ran out — Pygg held every Gemini model and moved the rest to GLM 5.3 without the agent seeing an error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Through the first sponsor's API credit, Nov 20:&lt;/strong&gt; a cascade of DeepSeek V4 Flash climbing to Qwen 3.8 Max only when the checks fail solved 49/51 at $0.034 per solved run. On 40 light tasks × 3, &lt;code&gt;auto/cheap&lt;/code&gt; learned each model's real answer length and cache use from the ledger and moved from Qwen 3.5 Flash ($1.91 per 1,000 correct) to DeepSeek V4 Flash ($0.23), same accuracy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Brain Storming, Nov 21:&lt;/strong&gt; a cheap model alone never built a working website (0 of 37 hidden API checks, three runs). Through Brain — a strong model planning, the cheap model doing the cards, climbing when a card's check fails — it passed 37/37 API and 20/21 UI checks, as good as the strong model alone. But it cost $5.78 and about two hours against $0.20–0.41 and 8–13 minutes: the cheap model read nothing from the prompt cache through that provider, and half its cards needed the strong model anyway. Brain makes a cheap model useful; here it did not make it cheap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Nov 23 proofs:&lt;/strong&gt; 20/20 staged limits resumed with the whole conversation; 50/50 unchecked results refused as not ready; 0 step-cap violations in 10 × 1,000 adversarial requests.&lt;/li&gt;
&lt;li&gt;Gateway overhead: about &lt;strong&gt;4 ms&lt;/strong&gt; per short request.&lt;/li&gt;
&lt;li&gt;From &lt;code&gt;npm install -g @pygg/cli@dev&lt;/code&gt; to a first answer: &lt;strong&gt;17.4 s&lt;/strong&gt; on a clean profile with local Ollama (the gateway itself answers in about 5 s; the rest is the install and Ollama loading the model).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The honest caveat: the Nov 20 overnight run gave each case one attempt, so a 36/36 is a single outcome, not a rate. Plan-quota routes cost no metered money but aren't comparable in dollars with metered ones. And I haven't measured savings on large refactors or migrations, and I don't claim a quality ranking of models.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it compares
&lt;/h2&gt;

&lt;p&gt;The repo keeps a comparison against LiteLLM, OpenRouter, 9router, OmniRoute and claude-code-router. Two labels: &lt;strong&gt;M&lt;/strong&gt; means measured here, against a fault-injecting fake provider; &lt;strong&gt;D&lt;/strong&gt; means taken from that project's own docs or source. Only LiteLLM was actually run — the rest are what their docs say on one day, so this is not a ranking.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Pygg&lt;/th&gt;
&lt;th&gt;LiteLLM&lt;/th&gt;
&lt;th&gt;OpenRouter&lt;/th&gt;
&lt;th&gt;9router&lt;/th&gt;
&lt;th&gt;OmniRoute&lt;/th&gt;
&lt;th&gt;claude-code-router&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Time added&lt;/td&gt;
&lt;td&gt;4 ms, ~230 req/s (M)&lt;/td&gt;
&lt;td&gt;2 ms, 1,170 req/s on 4 instances (D)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Primary failing: served&lt;/td&gt;
&lt;td&gt;100% (M)&lt;/td&gt;
&lt;td&gt;100% (M)&lt;/td&gt;
&lt;td&gt;Any error, &lt;code&gt;models&lt;/code&gt; list (D)&lt;/td&gt;
&lt;td&gt;401, 402, 403, 429, 5xx (D)&lt;/td&gt;
&lt;td&gt;Not on auth errors (D)&lt;/td&gt;
&lt;td&gt;Per rule (D)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Attempts when rate-limited&lt;/td&gt;
&lt;td&gt;1.04, ~70 ms (M)&lt;/td&gt;
&lt;td&gt;4, ~4.6 s (M)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Honours &lt;code&gt;Retry-After&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yes (M)&lt;/td&gt;
&lt;td&gt;Up to 60 s (D, M)&lt;/td&gt;
&lt;td&gt;Passes it on (D)&lt;/td&gt;
&lt;td&gt;No, fixed (D)&lt;/td&gt;
&lt;td&gt;On 429 (D)&lt;/td&gt;
&lt;td&gt;Yes (D)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Re-sends a request that may already have run&lt;/td&gt;
&lt;td&gt;No by default (M)&lt;/td&gt;
&lt;td&gt;Up to 3 attempts (M)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;On 5xx, timeouts (D)&lt;/td&gt;
&lt;td&gt;Up to 3 (D)&lt;/td&gt;
&lt;td&gt;When set (D)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session stays on one key&lt;/td&gt;
&lt;td&gt;Default, 60/60 (M)&lt;/td&gt;
&lt;td&gt;Opt-in: 0/60 off, 60/60 on (M)&lt;/td&gt;
&lt;td&gt;Default (D)&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;smart&lt;/code&gt; only (D)&lt;/td&gt;
&lt;td&gt;Strategy (D)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prompt cache, OpenAI-format on Claude&lt;/td&gt;
&lt;td&gt;Automatic, 89.5% (M)&lt;/td&gt;
&lt;td&gt;Opt-in: 89.5% / 39.7%; 0% default (M)&lt;/td&gt;
&lt;td&gt;Client sends &lt;code&gt;cache_control&lt;/code&gt; (D)&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;td&gt;—&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Web sessions&lt;/td&gt;
&lt;td&gt;6 sites, guard always on (M)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;6 sites, no pacing found (D)&lt;/td&gt;
&lt;td&gt;3 sites (D)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;It's a developer preview (v0.1.0-dev.9, MIT, Rust + Tauri). The Windows desktop app is a prerelease installer, not code-signed yet, so SmartScreen will warn. The CLI is on npm for Windows, macOS and Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm install -g @pygg/cli@dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;code&gt;pygg setup codex&lt;/code&gt; / &lt;code&gt;pygg setup opencode&lt;/code&gt; work the same way as Claude Code, and any OpenAI- or Anthropic-compatible SDK can use &lt;code&gt;http://127.0.0.1:8741/v1&lt;/code&gt; with model &lt;code&gt;auto&lt;/code&gt;. Free tiers work — Gemini, OpenRouter and Groq all have one, and Ollama runs local.&lt;/p&gt;

&lt;p&gt;One thing I want to be straight about: no outside user has run it yet. It's not for unattended or financially critical use. If you try it, I'd genuinely like to know where the routing helps and where it gets in the way. There's a paired study in the README — 6 tasks from your own backlog, 3 your usual way, 3 through Pygg, compared blind. Everyone who finishes gets credited.&lt;/p&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/nambean/Pygg" rel="noopener noreferrer"&gt;https://github.com/nambean/Pygg&lt;/a&gt;&lt;/p&gt;




</description>
      <category>ai</category>
      <category>llm</category>
      <category>opensource</category>
      <category>rust</category>
    </item>
  </channel>
</rss>
