Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
Python & Django

Wrapping a Model in Something Dependable

Last updated:

The service is more than the model call

Calling a model is a few lines. Making that call dependable — retried, validated, logged, capped and observable — is the actual work, and it is what separates a demonstration from a system.

The model call is perhaps five per cent of an AI service. The rest is everything that makes it safe to depend on.

The shape that works

  1. Receive the request and validate it
  2. Queue it, unless a person is genuinely waiting
  3. Build the prompt from configuration, not from code
  4. Call with a timeout and bounded retries
  5. Validate the output against a schema before accepting it
  6. Record input, context, output, model version and confidence

Force structured output

Ask for a defined structure and validate what comes back. If it does not match, retry once with a stricter instruction and then quarantine rather than accepting something malformed.

  • A schema the output must satisfy
  • Validation before anything downstream sees it
  • One retry with a stricter prompt
  • Quarantine with the raw response for diagnosis

Cost control is not optional

ControlWhy
Hard daily capA loop must not spend the budget
Per-caller capOne consumer cannot exhaust it
Cost per request trackedRising cost is the early warning
Token limits on outputGeneration is the expensive direction
Decided behaviour at the capDegrade or queue, not crash

Keep the prompts in configuration

Prompts change more often than code and are frequently adjusted by someone who is not a developer. They belong in configuration, versioned, with the ability to roll back.

That also makes it possible to run an evaluation set against a prompt change before deploying it, which is what stops quality regressions.

Frequently asked questions

Should the service call the model synchronously?

Only where a person is waiting. Everything else should be queued, which gives you retries and isolates provider slowness.

How do we handle provider outages?

Queue and retry for background work. For interactive work, degrade to a smaller model or fail with a clear message and a manual route.

Do we need to record everything?

Input, retrieved context, model version, output and confidence. Without those you cannot explain a decision later.

Which Python framework?

Any competent one. The architecture matters considerably more than the framework choice.

Keep reading

Building a service around a model?

The model call is the easy part. Happy to review the architecture around it.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Custom Software DevelopmentWeb DevelopmentMachine Learning