Entry 07AI infrastructure · cost2026

A gateway that routes coding-agent tasks across several models

A coding agent does not need a frontier model (the most capable and most expensive tier) for every call, because much of its work is mechanical. I built a personal gateway that connects one agent front-end to a subscription frontier model and to cheaper open models such as DeepSeek, GLM and Kimi. No API keys are stored in config files on disk, and a live display shows what the usage costs.

modelsClaude, DeepSeek, GLM, Kimi
clientsagent front-end, OpenCode
keys in config filesnone
spend historyPostgres

Why calls go to different models

A subscription frontier model suits hard reasoning. Much of what an agent does, such as searching a repository, summarizing code changes or making the same simple edit in many files, can be done by a cheaper model. Sending all of that work to the frontier model costs more than it needs to, and picking a model by hand before every call would take away much of the convenience of using an agent. The gateway lets each type of task go to a model chosen for it in advance.

How requests reach the models

LiteLLM runs as an OpenAI-compatible proxy: a server that accepts requests in the format of OpenAI’s API and forwards them to whichever provider serves the requested model. It sits in front of every provider I use. The agent talks to one endpoint whichever model answers.

A router maps task types to models, as in the config shown here. Separately, it lets me switch provider and model mid-session without restarting the agent. A second agent front-end, OpenCode, runs with a browser interface against the same pool of models, so the setup works with more than one client.

router config
routes:
  default:      frontier/claude
  bulk-edit:    open/deepseek-chat
  summarize:    open/glm
  quick-search: open/kimi

# keys injected at runtime, never written here
providers:
  frontier: ${FRONTIER_KEY}
  open:     ${OPEN_KEY}

Bulk edits, summaries and quick searches go to the open models. The frontier model is the default for everything else.

How spend is tracked

A status line on every prompt shows live spend against a budget for each API key. It is green while there is headroom, amber near the limit and red over it.

status line
[frontier: $3.40 / $10.00]  [open-pool: $0.85 / $25.00]

A small poller (a script that runs on a timer) copies each key’s spend into a Postgres database table every few minutes. The cost history therefore survives dashboard refreshes and can be queried over time, for example to compare spending before and after a routing change.

How API keys are handled

  • The gateway and router config files contain no plaintext keys. They reference keys by name.
  • At process start, the keys are pulled from a secret broker (a separate service that stores them) and held only in memory.
  • As an extra safeguard, the config is rewritten to its keyless stub when the process ends, so the config file at rest never contains a key.

The config files can therefore be committed to a repository without leaking a key.

AI Infra LiteLLM Observability Python

Questions or a use case in mind? Email is best — [email protected] · Back to portfolio