cd /news/ai-infrastructure/per-branch-ai-endpoints-isolating-mo… · home topics ai-infrastructure article
[ARTICLE · art-89568] src=dev.to ↗ pub= topic=ai-infrastructure verified=true sentiment=· neutral

Per-Branch AI Endpoints: Isolating Model Spend Across Prod, Preview, and CI

A developer from The DevOps Daily demonstrated a method to isolate AI model spend across production, preview, and CI environments using Neon's branch-based Postgres. By logging usage to a per-branch ledger, each environment records its own token counts, preventing CI or preview runs from affecting production's numbers. The approach was tested by making calls on a CI branch and observing that production's ledger remained unchanged.

read3 min views1 publishedAug 9, 2026

AI spend is hard to see. In most setups the same gateway credential is used by production, every preview environment, CI, and whatever load test someone ran on Friday. All of that lands in one undifferentiated number. You cannot answer "what did that preview cost," you cannot cap a specific environment, and you find out a CI job went into a retry loop against an expensive model when the monthly invoice arrives, not when it happens.

The reason is that spend is attributed to a key, and the key is shared. Neon changes what is shared: each branch is its own deployment, and if you log usage to Postgres, that ledger lives on the branch too. So a preview or CI branch records its own spend in its own ledger, and none of it moves production's numbers. I tested it by running calls on a CI branch and watching production's ledger stay flat. The repo is at the end.

ci-run

branch raised the branch's token count while production's ledger stayed exactly where it was.us-east-2

)When production and every ephemeral environment authenticate with the same credential, the provider's dashboard shows you one line. That has real consequences:

Tagging requests helps a little, but it is bookkeeping bolted on after the fact, and it still shares one budget and one rate limit.

On Neon each branch is its own deployment with its own function URL, and because you log usage to Postgres and Postgres branches, the usage ledger is per branch too. A call made against a branch's function URL writes to that branch's usage_log

, and that ledger is what makes spend attributable per environment: production's ledger is a different table on a different branch. The isolation demonstrated here is that per-branch ledger in Postgres, not a claim that Neon meters the gateway credential itself separately per branch. That distinction matters: the attribution you can rely on is the one you record yourself, in the branch's database.

The usage view is an ordinary query over that branch's log:

// GET /usage: tokens grouped by model, from THIS branch's log
const rows = await db
  .select({
    model: usageLog.model,
    calls: sql`count(*)::int`,
    totalTokens: sql`sum(${usageLog.totalTokens})::int`,
  })
  .from(usageLog)
  .groupBy(usageLog.model);

I read production's usage, branched a ci-run

environment, made two model calls against the branch, and read both ledgers.

The branch's gpt-5-nano

total went from 25 to 71 as its two calls landed, while production stayed at 25. The CI run's spend was recorded against the CI branch and nowhere else, and deleting the branch takes its ledger with it.

Because storage is copy-on-write, a new branch inherits production's ledger as it was at branch time (that is why the branch started at 25, not 0). The isolation is in the delta: everything spent on the branch after it is created stays on the branch, and nothing the branch does changes production's numbers. For clean per-run attribution, read the branch's growth, or keep CI branches short-lived so their ledger is just that run.

The gateway function with the per-branch usage log is here:

https://github.com/The-DevOps-Daily/neon-ai-gateway-demo

Model spend is only invisible because it is attributed to a shared key. Move the usage ledger onto the branch and the picture inverts: every environment keeps its own record, a preview or CI run spends against itself, and production's numbers are unaffected by anything a branch does. You get per-environment attribution and containment for free, and cleanup is the same delete a branch

that already tears down the rest of the preview.

── more in #ai-infrastructure 4 stories · sorted by recency
── more on @neon 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/per-branch-ai-endpoi…] indexed:0 read:3min 2026-08-09 ·