<?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: Qosway</title>
    <description>The latest articles on DEV Community by Qosway (@qosway).</description>
    <link>https://dev.to/qosway</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%2F4244609%2F9c396ba0-2ffb-46d9-9a2c-0085e113aa36.png</url>
      <title>DEV Community: Qosway</title>
      <link>https://dev.to/qosway</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/qosway"/>
    <language>en</language>
    <item>
      <title>Controlling Claude Code and Codex costs with a self-hosted gateway</title>
      <dc:creator>Qosway</dc:creator>
      <pubDate>Wed, 18 Nov 2026 07:24:16 +0000</pubDate>
      <link>https://dev.to/qosway/controlling-claude-code-and-codex-costs-with-a-self-hosted-gateway-4nj1</link>
      <guid>https://dev.to/qosway/controlling-claude-code-and-codex-costs-with-a-self-hosted-gateway-4nj1</guid>
      <description>&lt;p&gt;Originally published at &lt;a href="https://qosway.com/blog/controlling-claude-code-codex-costs/" rel="noopener noreferrer"&gt;https://qosway.com/blog/controlling-claude-code-codex-costs/&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Controlling Claude Code and Codex costs with a self-hosted gateway
&lt;/h1&gt;

&lt;p&gt;Teams that roll out Claude Code or Codex often see the cost after the fact, on a provider invoice, not in the client. The tools stay open. Sessions get long. The same repo context gets sent again. The model the client asked for is the model that gets called, even when the prompt is short and ordinary.&lt;/p&gt;

&lt;p&gt;Claude Code cost control and Codex usage limits do not have to wait for that invoice. You can put a gateway in your own network, point the clients at it, and decide per request whether it may leave, whether it needs to, and which model should answer.&lt;/p&gt;

&lt;p&gt;That is what Qosway is: a self-hosted LLM gateway, one static Go binary, in front of Claude Code, Codex, and any OpenAI-compatible client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the spend comes from
&lt;/h2&gt;

&lt;p&gt;Three patterns show up once a team puts coding agents in front of developers.&lt;/p&gt;

&lt;p&gt;First, headcount. One person experimenting is a different traffic shape from a desk full of developers, each with the client installed and a provider key.&lt;/p&gt;

&lt;p&gt;Second, long agent sessions. These tools do not complete a function and stop. They read files, call tools, retry, and carry conversation history forward. Token use is not "one prompt, one completion."&lt;/p&gt;

&lt;p&gt;Third, repetition. The same error, the same "what does this module do," the same boilerplate question gets asked more than once â€” by the same person later in the day, or by a colleague who was not in the first thread. If every one of those hits the provider, you pay for each.&lt;/p&gt;

&lt;p&gt;The useful controls are the ones that sit in front of the provider call: routing, cache, quotas, and a way to actually look at usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Claude Code cost control, without changing the client
&lt;/h2&gt;

&lt;p&gt;Claude Code speaks Anthropic Messages. Qosway accepts that shape, and the client keeps its own response shape. You do not rewrite the tool. You point it at a gateway running inside your network.&lt;/p&gt;

&lt;p&gt;Cost control here is mostly about not sending every request to the model the client named. In Qosway, the model a client asks for is a request, not an order. A routing policy can move short everyday prompts that arrived on a premium model to the cheapest enabled model, while leaving technical, very long, and tool-using prompts on the model that was requested. You can also pin all traffic to one provider and model, split it with weights, write your own prompt rules (a pattern, a token range, a named model), or just use what the client sent.&lt;/p&gt;

&lt;p&gt;OpenAI, Anthropic, DeepSeek and Grok are each one switch. Turn a provider off and it leaves those choices. If a provider answers 429 or 5xx, the request moves to a fallback model. Responses stream as they arrive.&lt;/p&gt;

&lt;p&gt;Every response also says how it got here â€” cache result, original model, model actually used, provider, and whether it was downgraded. The product page shows this as response headers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proxy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;          &lt;span class="no"&gt;MISS&lt;/span&gt;
&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proxy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Original&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;claude&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;opus&lt;/span&gt;
&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proxy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;          &lt;span class="n"&gt;deepseek&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;
&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proxy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;       &lt;span class="n"&gt;deepseek&lt;/span&gt;
&lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Proxy&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Downgraded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The client itself will not tell you that a request was rerouted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Codex usage limits, applied before the provider is called
&lt;/h2&gt;

&lt;p&gt;Codex, and any OpenAI-compatible client, can be pointed at the same binary. Usage limits only work if they are enforced before the upstream call. After the provider has already run the model, a quota is just a record of spend that already happened.&lt;/p&gt;

&lt;p&gt;Qosway checks the client key and applies a rate limit first. A bad key is 401 invalid_api_key. A key over its rate limit is 429 rate_limit_error. The default rate limit is a fixed window per key, 600 requests a minute, counted in Redis. Then the gateway reserves tokens against that key's daily and monthly caps. Over the cap is HTTP 429 quota_exceeded, returned before the upstream call. Defaults apply to every key; per-key overrides take effect immediately, and you can change them in the panel without a redeploy.&lt;/p&gt;

&lt;p&gt;There is also a hard size cap: bodies above 10 MiB are refused with 413.&lt;/p&gt;

&lt;p&gt;The binary is closed by default. With no client key created, every request is rejected. A gateway with an open front door is not a usage-limit tool.&lt;/p&gt;

&lt;p&gt;The order of these checks is the order in the code. Identify the key, inspect the prompt for secrets, reserve quota, then route. A request can leave the line at any of those stops, with a named error back to the client, before a provider is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exact and semantic cache
&lt;/h2&gt;

&lt;p&gt;A repeated question should not be paid for twice. After routing, Qosway looks for the same question: exact match first, then semantic. A hit is answered at the gateway â€” HIT-EXACT or HIT-SEMANTIC â€” and there is no provider call.&lt;/p&gt;

&lt;p&gt;Exact cache is a SHA-256 of model, messages and tools, held in Redis. Same request, same answer.&lt;/p&gt;

&lt;p&gt;Semantic cache uses cosine similarity on the last user message. The default threshold is 0.95. It is skipped when the request carries tools, which is the common case for an agent in the middle of a coding session. The cache is shared across the company, but a hit needs the same prior conversation, so one thread is never served as the answer to another.&lt;/p&gt;

&lt;p&gt;The semantic cache holds answers and embeddings, not prompt text. Exact entries hold full answers and are not encrypted, so Redis should have a password or TLS on a private network. If Redis goes down, the exact cache and the rate limit switch off, and traffic keeps flowing to the providers. The process is built to degrade rather than stop.&lt;/p&gt;

&lt;p&gt;Semantic matching uses a local embedder unless an OpenAI key is configured; then embeddings go to OpenAI. That is worth knowing if part of the reason you self-host is to keep that traffic on the network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seeing usage: the panel and Prometheus metrics
&lt;/h2&gt;

&lt;p&gt;Controls without visibility turn into arguments about what happened last week. The admin panel is built into the binary. It shows per-key usage, cost, cache results and request history. The same process exposes a Prometheus &lt;code&gt;/metrics&lt;/code&gt; endpoint, plus &lt;code&gt;/healthz&lt;/code&gt; and &lt;code&gt;/readyz&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Usage, cost, cache result and latency are written off the response path, so recording is not sitting on the hot return.&lt;/p&gt;

&lt;p&gt;The records reference a key id, never the key. Events are kept 90 days, daily buckets 35 days, monthly buckets 14 months, then pruned. That is the retention the product states, not a compliance claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  Egress secret blocking
&lt;/h2&gt;

&lt;p&gt;This is not a cost feature, but it runs in the same path, before anything is cached or sent. Coding agents see whatever is in the working tree, and prompts are a common way for a key or a password to leave the machine.&lt;/p&gt;

&lt;p&gt;The gateway scans the prompt for secrets: private keys, AWS access keys, GitHub tokens, Slack tokens, Stripe live keys, Google API keys, OpenAI and Anthropic keys, database URLs with a password, assigned passwords and API keys, and JSON Web Tokens. All ten rules are on by default; each can be switched off. You can add your own phrases or RE2 patterns, and an allow-list for exact values.&lt;/p&gt;

&lt;p&gt;A blocked request returns 403 egress_blocked with the rule name. The matched text is not logged, cached or returned.&lt;/p&gt;

&lt;p&gt;Names and email addresses are not blocked by default. The product does not read meaning: egress rules are patterns, and routing is rules, not a model judging your prompts. It also does not block personal data by default, on the grounds that in code tools that is mostly false alarms. If you need that, you add your own patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it does not do
&lt;/h2&gt;

&lt;p&gt;It does not terminate TLS. Put it behind a reverse proxy, or bind it to loopback.&lt;/p&gt;

&lt;p&gt;It is one binary, not a stack of moving parts: no Node runtime, no separately operated database server. SQLite lives in the data directory. You run it as a systemd service or a container, with the TLS reverse proxy you already operate.&lt;/p&gt;

&lt;p&gt;Each binary carries a license signed for one customer and will not start without a valid one. The license is verified at start and re-checked every five minutes. Outbound traffic, as listed on the site, is the providers you enable and the license check â€” plus embeddings to OpenAI if you configure that key for semantic matching.&lt;/p&gt;

&lt;p&gt;Provider keys, client keys, the license key and the admin password hash sit in SQLite, encrypted with AES-GCM, using a key file kept beside the database. The site is explicit that you should store that file separately from backups of the database. Cached answers are not encrypted.&lt;/p&gt;

&lt;p&gt;On first run, the admin password is set from the same machine, or with a one-time token printed in the log. Repeated failed admin logins from one address are locked out for a growing time. A stream that ends early is reported as an error and is never cached.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you want to look at your own traffic
&lt;/h2&gt;

&lt;p&gt;The product page is at &lt;a href="https://qosway.com" rel="noopener noreferrer"&gt;qosway.com&lt;/a&gt;. If you want to see the route your own traffic would take, the site asks you to say which clients and providers you run today, and which rules you would want on day one.&lt;/p&gt;

&lt;p&gt;Request a demo â€“ &lt;a href="mailto:hello@qosway.com?subject=Qosway%20demo%20-%20devto"&gt;hello@qosway.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>devops</category>
      <category>security</category>
      <category>llm</category>
    </item>
  </channel>
</rss>
