Skip to content
bossfight
.dev
learn
problems
guides
bosses
roadmap
practice
Interview mode
Live coding rounds with a clock
Gauntlet
Tiny problems back to back: spot it, code it
Quiz
Quick checks on every topic
Which technique?
Name the pattern before you code
Estimation drills
Back-of-envelope numbers for system design
By company
Work through one company's set
Mock assessments
Timed company sets, no hints
Codebase rounds
Fix and extend a real repo
progress
sign in
Search
Ctrl K
menu
Learn
Learn (step by step)
Guides
Roadmap
Practice
Problems
Interview mode
Gauntlet
Quiz
Which technique?
Estimation drills
By company
Mock assessments
Codebase rounds
Play
Boss fights
You
Progress
Sign in
~/rounds
Inference gateway
medium
60 min
Python
Meta AI-enabled coding-style
Atlassian-style
1
Four times the same bad request
2
Build a feature
3
Make it fast
repo
infer/
__init__.py
batcher.py
clock.py
errors.py
fake_model.py
gateway.py
models.py
scheduler.py
tokens.py
tests/
visible/
test_gateway.py
test_retries.py
helpers.py
README.md
README.md
read-only
# infer A small gateway in front of a language model. Tenants (our customers) submit prompts; the gateway queues them, packs them into batches, sends each batch to the model server and keeps the results. infer/models.py Request, Completion, Result infer/tokens.py count_tokens(): a rough count, one token per word infer/errors.py ModelError: what the model server raises, with its HTTP status infer/clock.py SystemClock, and FakeClock for tests (sleep() just moves time on) infer/fake_model.py FakeModel: the stand-in model server we develop and test against infer/scheduler.py FairQueue: waiting requests, shared fairly between tenants infer/batcher.py Batcher: sends one batch, retrying when that can help infer/gateway.py Gateway: submit(), flush(), result() How a request goes through: 1. `submit(tenant, prompt, max_tokens)` checks the request (not empty, fits in the model's context) and queues it. Bad requests are `"rejected"` at once. 2. `flush()` takes batches off the queue until it's empty. A batch holds at most `max_batch` requests whose costs (prompt tokens + `max_tokens`) add up to at most `max_batch_tokens`. 3. Batches are filled **fairly**: tenants take turns, one request each, oldest first (see `FairQueue`). One tenant with a huge backlog can't starve others. 4. The `Batcher` sends the batch. Errors the model server says are temporary (429, 5xx) are retried with backoff (0.5 s, 1 s, 2 s...); any other error fails the batch straight away. Every request in a batch that fails gets a `"failed"` result. Run the tests with `python -m pytest` (or press Run in the browser).
esc