<?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: page6668</title>
    <description>The latest articles on DEV Community by page6668 (@page6668).</description>
    <link>https://dev.to/page6668</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%2F4184055%2F1c60d5cc-4c9d-495a-aa6a-36c0fc63013d.png</url>
      <title>DEV Community: page6668</title>
      <link>https://dev.to/page6668</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/page6668"/>
    <language>en</language>
    <item>
      <title>I built an OpenAI-compatible gateway for 6 Chinese LLMs — what the happy path hides</title>
      <dc:creator>page6668</dc:creator>
      <pubDate>Wed, 14 Oct 2026 08:03:28 +0000</pubDate>
      <link>https://dev.to/page6668/i-built-an-openai-compatible-gateway-for-6-chinese-llms-what-the-happy-path-hides-3875</link>
      <guid>https://dev.to/page6668/i-built-an-openai-compatible-gateway-for-6-chinese-llms-what-the-happy-path-hides-3875</guid>
      <description>&lt;p&gt;Two months of full-time work went into this. The part that nearly sank it was not&lt;br&gt;
the routing layer, the Kafka pipeline, or the admin console. It was token&lt;br&gt;
accounting.&lt;/p&gt;

&lt;p&gt;I built He-API: a self-hosted gateway that puts six Chinese model providers&lt;br&gt;
(DeepSeek, Kimi, Qwen, Doubao, ERNIE, GLM) behind a single OpenAI-compatible&lt;br&gt;
endpoint. The thesis is boringly practical — Chinese models run 80–90% cheaper&lt;br&gt;
than GPT-4/Claude-class Western models for most workloads, but if you're a&lt;br&gt;
developer outside China, actually using them is painful: docs in Chinese,&lt;br&gt;
payment methods that don't work internationally, inconsistent auth, no shared&lt;br&gt;
format.&lt;/p&gt;

&lt;p&gt;So: one endpoint, one key, one request shape. Here's the architecture, and then&lt;br&gt;
the parts that cost me the most time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The shape of it
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client (any OpenAI SDK)
  → Nginx + Let's Encrypt
  → api-gateway (Go)
      → entitlement resolver   (Redis)
      → rate limiter           (Redis + Lua)
      → model router
      → provider adapter       (kimi / deepseek / qwen / doubao / ernie / glm)
      → upstream provider
  → Kafka → ClickHouse   (usage events)
  → PostgreSQL           (billing, models, providers)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;15 services on systemd — all Go except a Next.js admin console. Postgres,&lt;br&gt;
Kafka, Redis, ClickHouse. 22 migrations, all idempotent.&lt;/p&gt;

&lt;p&gt;That's the diagram everyone draws. Here's what actually bit.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. "OpenAI-compatible" is 90% edge cases
&lt;/h2&gt;

&lt;p&gt;The happy path — a non-streaming completion — took an afternoon. Everything&lt;br&gt;
after that was tails.&lt;/p&gt;

&lt;p&gt;Streaming is where it starts. Providers don't agree on chunk shape. Some send&lt;br&gt;
&lt;code&gt;data: [DONE]&lt;/code&gt;, some just close the connection. Some include a &lt;code&gt;usage&lt;/code&gt; block in&lt;br&gt;
the final chunk; some omit it entirely on streams and only report usage on&lt;br&gt;
non-streaming calls. If you're building a gateway, you discover that&lt;br&gt;
"compatible" means "compatible for the paths you happened to test."&lt;/p&gt;

&lt;p&gt;Then error shapes. A 400 from one provider is a JSON body with an &lt;code&gt;error.message&lt;/code&gt;&lt;br&gt;
field; from another it's a bare string; from a third it's a 200 with the error&lt;br&gt;
inside the body. Your gateway has to normalize all of that into something that&lt;br&gt;
doesn't break the client's existing error handling — otherwise "zero code&lt;br&gt;
changes" is a lie the first time a provider sneezes.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Usage accounting is a schema problem, not a code problem
&lt;/h2&gt;

&lt;p&gt;This is the one that nearly sank it.&lt;/p&gt;

&lt;p&gt;If you bill per token, the token count is money, and money is never a float.&lt;br&gt;
Prices land around ¥0.1–¥2 per million tokens, which means per-request amounts&lt;br&gt;
are genuinely small — and if you do the arithmetic in float64 you will drift,&lt;br&gt;
and your drift will compound across millions of rows.&lt;/p&gt;

&lt;p&gt;So: &lt;code&gt;NUMERIC&lt;/code&gt; in Postgres, integer micro-units in the hot path, and a deliberate&lt;br&gt;
decision about where rounding happens. Then, on top of that, the reconciliation&lt;br&gt;
problem — because providers report usage inconsistently (see above), you can't&lt;br&gt;
just trust the upstream number. The policy I settled on is stricter than estimating: if the stream never delivers a usage chunk, nothing is charged at all. That keeps estimates out of the money path — at the cost of the ledger being unable to tell you about the attempts it declined to bill, which is a gap I'd close with an explicit usage_source column and a record of unmetered attempts&lt;/p&gt;

&lt;p&gt;The lesson: decide your money representation before you write the first billing&lt;br&gt;
line, not after you find a 0.3% discrepancy you can't explain.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Hot reload beats redeploys
&lt;/h2&gt;

&lt;p&gt;Provider keys used to require a restart. That meant a redeploy — and therefore a&lt;br&gt;
window where requests fail — every time a key rotated.&lt;/p&gt;

&lt;p&gt;The fix is unglamorous and it removed an entire class of operations pain: the&lt;br&gt;
gateway polls an internal &lt;code&gt;active providers&lt;/code&gt; endpoint every 30 seconds and swaps&lt;br&gt;
the in-memory provider set atomically. Keys are encrypted at rest with&lt;br&gt;
AES-256-GCM, decrypted only in memory. Rotating a key is now "save in the admin&lt;br&gt;
console, wait 30 seconds, done."&lt;/p&gt;

&lt;p&gt;If you're building anything that talks to multiple third-party APIs: build the&lt;br&gt;
reload path before you need it. Ops pain compounds faster than feature requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Rate limiting needs two axes, not one
&lt;/h2&gt;

&lt;p&gt;My first limiter was a single per-key QPS number. It failed in a way that took&lt;br&gt;
me a while to see: internal sub-requests were consuming the same budget as the&lt;br&gt;
user-facing request, so a free-tier account with QPS=1 rate-limited itself.&lt;/p&gt;

&lt;p&gt;The version that works has two axes — a steady per-key rate plus a burst bucket,&lt;br&gt;
with burst size scaled by account age and spend. New accounts get a tiny burst;&lt;br&gt;
accounts with history get a generous one. It's crude, but it kills almost all&lt;br&gt;
automated abuse without touching legitimate power users.&lt;/p&gt;

&lt;p&gt;Gate the counter itself carefully, too. A read-then-write in application code&lt;br&gt;
races under load; that check-and-increment belongs in a single atomic Lua script&lt;br&gt;
against Redis.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The &lt;code&gt;base_url&lt;/code&gt; trap
&lt;/h2&gt;

&lt;p&gt;Small, embarrassing, worth writing down. Most OpenAI SDKs concatenate paths onto&lt;br&gt;
your &lt;code&gt;base_url&lt;/code&gt;, so if you document &lt;code&gt;https://api.example.com&lt;/code&gt; and the SDK appends&lt;br&gt;
&lt;code&gt;/v1/chat/completions&lt;/code&gt;, you 404. Some SDKs want the host, some want it with&lt;br&gt;
&lt;code&gt;/v1&lt;/code&gt;. Get this wrong and your very first integration attempt fails for a reason&lt;br&gt;
that has nothing to do with your actual product.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write the billing correctness tests first.&lt;/strong&gt; Not because billing is
interesting, but because everything else is cheap to change and that isn't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assume every upstream field is optional.&lt;/strong&gt; Design the normalization layer
around the worst reporter, then optimize for the good ones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't self-host ClickHouse for a project this size.&lt;/strong&gt; It's excellent and it
was the right call on paper. For a two-person team it's one more thing to keep
alive at 2am.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ask the distribution question on day one.&lt;/strong&gt; More below.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where it is now
&lt;/h2&gt;

&lt;p&gt;Honest status: it works, it's verified end-to-end (real upstream call → real&lt;br&gt;
model response → HTTP 200, captured in the repo), and it has &lt;strong&gt;zero users and&lt;br&gt;
zero revenue&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I built it. I did not distribute it. Those are different skills and I'm only&lt;br&gt;
good at one of them — which is exactly the thing nobody tells you when they say&lt;br&gt;
"just build it and they will come."&lt;/p&gt;

&lt;p&gt;The code is public, and the notes above are the parts I'd have wanted someone to&lt;br&gt;
hand me:&lt;/p&gt;

&lt;p&gt;→ &lt;a href="https://github.com/page6668/He-API" rel="noopener noreferrer"&gt;https://github.com/page6668/He-API&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you're building something similar, I'm happy to answer questions in the&lt;br&gt;
comments — especially on the usage-accounting and hot-reload parts, which are&lt;br&gt;
where I'd do it differently a second time.&lt;/p&gt;

&lt;p&gt;And if you're based in Southeast Asia or the Middle East and want to run an API&lt;br&gt;
reselling business on top of this cost advantage, the whole thing — source,&lt;br&gt;
live environment, domain, ops scripts, handover docs — is available. Details by&lt;br&gt;
email; it's in my profile.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>go</category>
      <category>api</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
