All posts

"Testing AI features without burning API credits: the provider seam"

If your AI feature can only be tested against a live API with a paid key, it will be under-tested, guaranteed. The fix is a provider seam: put the model call behind an interface, run CI against a deterministic mock that includes the failure paths, add recorded fixtures for realism, and keep one small live smoke test on a schedule.

AI engineering5 min read16 July 2026by Ahmed
"Testing AI features without burning API credits: the provider seam"

If the only way to test your AI feature is to hit a live API with a paid key, it will be under-tested. Not might be, will be. I have watched the same quiet calculation happen on every delivery team I've led: this test run costs money, takes seconds or tens of seconds per call, needs a secret that may or may not be in my environment, and might fail for reasons that have nothing to do with my code. So developers run the tests less often, or skip the AI paths entirely, or mark them "manual only". The coverage report says one thing; the truth is that the most fragile part of the system is the least exercised.

The fix is old-fashioned and unglamorous: a provider seam. The model call sits behind an interface, one method, or a small handful, that takes your request shape and returns your response shape. Nothing else in the application imports the vendor SDK directly. In CI and local development, the interface is satisfied by a deterministic mock provider that returns canned or rule-based responses. The real provider is swapped in by configuration, an environment variable, a factory, a dependency-injection binding. Your entire test suite now runs fast, free and offline, on every push, on every laptop, with no secrets.

The mistake most teams make with the mock is only teaching it the happy path. A mock that always returns a well-formed answer tests almost nothing, because well-formed answers are the one case that was already working in the demo. The mock earns its keep on the failure paths: JSON with a missing field, JSON wrapped in markdown fences, prose when you asked for structure, a refusal, a truncated output that stopped mid-object, a timeout, a 429, a 500. Every one of those will happen in production. If your parsing, validation, retry and fallback logic has never seen them in a test, you are finding out at runtime, in front of a user.

"Testing AI features without burning API credits: the provider seam"

A rule-based mock covers a lot: if the prompt contains X, return fixture Y; if a test sets a flag, throw a timeout. But there is a useful middle tier between fully synthetic mocks and live calls, recorded fixtures. Run the real provider once, capture the actual responses, commit them, and replay them in tests. The tooling for this has existed for over a decade, VCR.py cassettes in Python, with equivalents in most ecosystems, and because the major provider SDKs let you override the API base URL, you can even run the fake as a small local HTTP server and point every process in your stack at it, streaming included. Real model output is messier than anything you would invent, and fixtures catch parsing assumptions your hand-written mocks were too polite to break. The trade-off is honest maintenance: fixtures go stale when you change prompts or models, so treat re-recording as a deliberate step, not an accident.

You still want some contact with reality, and this is where teams overcorrect in the other direction and put live calls back into CI. Don't. Keep one small live smoke test, a handful of representative calls asserting the loosest possible contract, roughly "the provider responds and the response parses", and run it on a schedule, nightly or hourly, not on every push. When the provider changes behaviour or your key expires, the smoke test tells you within hours, without making every pull request slow, flaky and expensive.

The seam pays a second dividend that has nothing to do with testing. When every model call goes through one interface, switching providers is a configuration change, not a rewrite. New model, different vendor, a self-hosted fallback for one workload, you implement the interface once and flip a setting. Teams that scattered SDK calls through the codebase discover the migration is a multi-week refactor; teams with a seam do it in an afternoon. Given how fast model pricing and quality move, that flexibility is worth having even if you never write a single test.

There is one conflation worth naming explicitly, because I see it constantly: testing your code and testing the model are different activities that need different machinery. The deterministic mock tests your code, given this response, does the parser parse, does the retry fire, does the fallback engage. It should be exact and boring. Whether the model actually produces good answers to real inputs is a different question, and that is what evals are for: a dataset of real cases, scored against expectations, tracked over time, run when the prompt or the model changes. Mixing the two gives you the worst of both, flaky unit tests and no real quality signal.

None of this is speculative. My public extraction agent at github.com/afareed007/docextract ships exactly this shape: a deterministic mock provider and the real one selected by a single environment variable, a full test suite that runs offline, failure paths included, and a separate eval harness that scores field-level precision and recall against golden documents and fails CI below a threshold. The whole suite runs in seconds, costs nothing, and has caught far more bugs than any live call ever did.

If you are building AI features and the honest answer to "how is this tested?" is "we try it manually with a real key", this is exactly the work I do, putting a proper seam, test suite and eval harness around production LLM systems. Get in touch via the contact form and I'll happily take a look at your setup.

Senior engineering roles — or production AI consulting?

I'm open to senior AI and software engineering roles and to selective consulting on agents, RAG, and LLM automation in production. If this post resonates — as a hiring manager or as someone shipping similar work — get in touch.

Get in touch

© 2026 Ahmed Fareed. All rights reserved.

LOADING