Skip to content

Route AI Requests with LiteLLM

Category AI Solutions Engineering
Status 🟡 Partially completed
Repo nextwork-route-ai-requests-with-litellm
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

📚 NextWork learning project. This is a practice project I built while working through NextWork's Route AI Requests with LiteLLM guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning › AI Solutions Engineering
Status: 🟡 Partially completed

Overview

An AI gateway sits between apps and models. It hands out scoped virtual keys, can route each request to a cheap or a strong model depending on how hard it looks, and records spend so a budget can stop runaway costs. I ran LiteLLM's version of all three locally and measured whether routing actually picked the right model.

Summary

A LiteLLM gateway runs locally in Docker with Postgres. My config prices two local Ollama models as small and big and adds two complexity routers (rule-based and model-based). My benchmark sent six prompts through each setup with a $1 virtual key. Fixed: everything on big. Rules router: fastest and cheapest, but it sent two hard prompts to small. Model router: right on the hard prompts, but it sent easy ones to big and paid a classifier fee on every request. Each setup right-sized 4 of 6. The budget guardrail (secret mission) refused call 8 with budget_exceeded. All torn down. Partial because Google Cloud, Vertex AI Gemini and the hosted Jev classifier need accounts I can't create.

The checks behind this are in docs/verification.txt.

Skills & tools

LiteLLM, AI gateways, Docker, PostgreSQL, Model routing, Cost controls

What I built

  • config.yaml (mine): small = qwen2.5:0.5b and big = qwen2.5:1.5b with made-up per-token prices. rules-router (heuristic) and model-router (small classifies first) both use tiers SIMPLE→small and MEDIUM/COMPLEX/REASONING→big, and fall back to big.
  • up.sh / down.sh: Postgres 16 and the LiteLLM proxy in Docker on host networking. The master key and DB password are generated into ~/.nw-litellm/, outside the repo. Dockerfile bakes my config into the stock image.
  • benchmark.py (mine): six prompts of my own (two easy, two medium, two hard), each labelled with the model I think it deserves. It sends them under one model name and records who answered, latency, response cost and classifier cost (docs/bench-*.csv, docs/run-*.txt).
  • compare.py → docs/comparison.md: the three runs side by side, plus the gateway's own /spend/logs rows for bench-key, which show the tier, cause (llm_classifier), signals and spend, with the classifier calls as separate records.
  • Results (3 Dec): fixed took 61 s and $0.00190. Rules took 29 s and $0.00117, but it sent dns and payments to small. The model router took 52 s and spent $0.00188 on answers plus $0.00031 on classifier calls; it sent the two easy prompts to big. None beat 4/6, so routing quality comes down to the classifier.
  • secret_mission.py → docs/secret-mission.txt: a capped-key limited to big with a $0.0008 budget. Calls 1–7 passed (spend is recorded a little late), and call 8 got HTTP 422 budget_exceeded at $0.000969. The key was deleted afterwards.
  • docs/cleanup.txt: both containers removed.

How to run

ollama pull qwen2.5:0.5b && ollama pull qwen2.5:1.5b
OLLAMA_API_BASE=http://127.0.0.1:11434 ./up.sh
set -a; . ~/.nw-litellm/.env.local; set +a; export LITELLM_BASE_URL=http://127.0.0.1:$(cat ~/.nw-litellm/.port 2>/dev/null || echo 4000)
# make a virtual key: POST /key/generate {"key_alias": "bench-key", "max_budget": 1}, then export LITELLM_API_KEY
python3 benchmark.py fixed && python3 benchmark.py rules && python3 benchmark.py model
python3 compare.py > docs/comparison.md && python3 secret_mission.py
./down.sh

Verification

I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.

/workspace/nextwork-portfolio/.venv/bin/python -m py_compile benchmark.py compare.py secret_mission.py && /workspace/nextwork-portfolio/.venv/bin/python -c "import yaml;c=yaml.safe_load(open('config.yaml'));n={m['model_name'] for m in c['model_list']};assert n=={'small','big','rules-router','model-router'},n" && bash -n up.sh && for m in fixed rules model; do test $(wc -l < docs/bench-$m.csv) -eq 7; done && grep -q budget_exceeded docs/secret-mission.txt && grep -q llm_classifier docs/comparison.md && echo LITELLM OK

Notes

Rewritten 3 Dec 2026: the earlier version kept the guide's benchmark script, prompts, aliases and image layout. Everything here is now mine, and the whole run (benchmarks, comparison, guardrail, teardown) was redone from scratch.

What isn't done, and why

The guide deploys on Google Cloud (Cloud Run, Cloud SQL, Artifact Registry, Secret Manager, Vertex AI Gemini 3.5 Flash/Flash-Lite) and routes with TypeSafe's hosted Jev classifier (classifier_type: jev). Both need accounts and billing I can't create, so here they're local Docker Postgres, the LiteLLM proxy, Ollama Qwen models with notional prices, and LiteLLM's own heuristic and LLM classifiers. Admin UI steps (virtual keys, Logs, Usage) were done through the same proxy API the UI calls, and the UI itself was only checked as reachable. The Terraform wasn't applied.


Original project: https://nextwork.ai/projects/cbaf6dfa-47e0-4c46-9e9f-2bd53103eaee · Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ /workspace/nextwork-portfolio/.venv/bin/python -m py_compile benchmark.py compare.py secret_mission.py && /workspace/nextwork-portfolio/.venv/bin/python -c "import yaml;c=yaml.safe_load(open('config.yaml'));n={m['model_name'] for m in c['model_list']};assert n=={'small','big','rules-router','model-router'},n" && bash -n up.sh && for m in fixed rules model; do test $(wc -l < docs/bench-$m.csv) -eq 7; done && grep -q budget_exceeded docs/secret-mission.txt && grep -q llm_classifier docs/comparison.md && echo LITELLM OK

LITELLM OK

[exit code 0]