LLM Gateway with Semantic Cache, Model Routing and Per-User Budgets
Teams that use language models in more than one feature soon have API keys scattered across services, no idea who spent what, and every request going to the most expensive model. Build an LLM gateway that sits between applications and model providers, with a semantic cache, routing by task difficulty, per-user budgets and full request logs. The core of the project is an evaluation set that proves the cheaper routing and the cache do not quietly lower answer quality.
Intermediate
A small team, or one strong student willing to learn something new.2 semesters · 2 to 3 students
7 to ship
4 optional extrasSuggested stack
What you should ship
- An OpenAI-compatible gateway endpoint with streaming and adapters for at least two model providers
- Per-application and per-user API keys with token and cost accounting and enforced daily and monthly budgets
- Exact-match and semantic caching with a configurable similarity threshold and per-user scoping
- A router that picks a model by task difficulty, in a rule-based and a learned version
- A request log and dashboard showing cost, latency, cache hits and routing decisions
- An evaluation set of at least 200 prompts with grading rubrics, and a script that scores any gateway configuration
- A results report comparing the always-large, routed, and routed with cache configurations on cost, latency and quality
If you have time left
- Redaction of personal data before prompts leave the gateway
- Automatic fallback to another provider on timeouts, with a circuit breaker
- A comparison between provider-side prompt caching and your own cache
- Quality monitoring that samples live traffic for grading
Problem
Most applications send every prompt to the strongest model because nobody has measured whether a cheaper one would do. Similar questions are paid for again and again. One enthusiastic user, or a bug in a loop, can burn a month's budget in an afternoon. Caching and routing can cut costs sharply, but without evaluation nobody knows what they did to quality.
What you build
An HTTP gateway that exposes one OpenAI-compatible chat endpoint to internal applications and forwards requests to two or more models, for example a small and a large model from a hosted provider and a local model through Ollama.
- Authentication and budgets: API keys per application and per user, token and cost accounting per request, daily and monthly limits, and either a clear error or a downgrade to a cheaper model when a limit is reached.
- Caching: exact-match caching first. Then a semantic cache: embed each prompt, look for an earlier prompt above a similarity threshold within the same scope, and return its answer.
- Model routing: a router that classifies each request as easy or hard and picks a model. Start with rules based on length and task type, then try a small classifier.
- Observability: every request logged with model, tokens, cost, latency, cache hit or miss and routing decision, and a dashboard over those logs.
- Evaluation: a fixed set of at least 200 prompts with reference answers or grading rubrics, covering the tasks your applications actually send.
Suggested architecture
- Python with FastAPI, or Node.js with TypeScript. The gateway is an ordinary web service and does not need an agent framework.
- PostgreSQL with pgvector for cache embeddings and request logs, and Redis for the exact-match cache. Keep dependencies small and write the provider adapters yourself.
- Stream responses through the gateway and count tokens as they pass.
- Run evaluation as a script that sends the whole set through three configurations: always the large model, the router, and the router with the cache. Score with rubric grading by a strong model, and check a sample by hand.
Milestones by month
- Months 1 and 2: gateway with two providers, API keys, streaming, request logging, first version of the evaluation set.
- Month 3: token and cost accounting, per-user budgets, dashboard.
- Month 4: exact-match cache, then the semantic cache with a tunable threshold, and its evaluation.
- Months 5 and 6: rule-based router, then a learned router, both evaluated against the always-large baseline.
- Month 7: load test, and handling a provider that is slow or down.
- Month 8: final evaluation, report and demo.
How it will be assessed
- A results table: cost per 1,000 requests, p95 latency and quality score for each configuration, with the size of any quality drop and how confident you are in it.
- A cache analysis: hit rate at several similarity thresholds, and the number of wrong answers served from the cache at each.
- Agreement between the automated grader and human judgement on a sample of at least 50 answers.
- A demo where a user reaches their budget and a provider outage triggers a fallback.
Pitfalls
- A semantic cache that treats "fee deadline for BS Computer Science" and "fee deadline for BS Software Engineering" as the same question. Tune the threshold on real examples, and never share cache entries across users when the answer depends on the user.
- Building the router before the evaluation set. Without the set, you cannot tell whether routing helps.
- Trusting an automated grader without checking it against people.
- Logging prompts that contain personal data without redaction or a retention limit.
Ideas and guidance, not finished projects
These are project ideas and scoping guidance, published free for students to use as a starting point. I do not build, write, or sell final-year projects, and I do not complete coursework for anyone. Take an idea, make it yours, and build it.
Not sure this one fits you?
I help students pick an idea that matches their skills and their deadline, then scope it down until it is finishable. Guidance only, never done for you.
Stuck on something specific?
Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.
Follow along
New writing, resources and project ideas land here first.