The Complete Guide to API Mocking for AI Applications: APIMock-AI in Practice

APIMock-AI uses six mock strategies, contract testing, and chaos engineering to make AI apps' external dependencies controllable and verifiable.
The article introduces APIMock-AI, an API mocking solution for AI applications. AI apps depend heavily on external services like LLMs, embeddings, and vector databases, and without mocks this leads to flaky CI, frequent rate limiting, runaway bills, and no offline development. APIMock-AI offers six response strategies—static, sequential, template, callback, proxy, and chaos—with ready-to-use builders for OpenAI, Anthropic, and others, supporting deterministic pseudo-vectors, SSE streaming, and sliding-window rate limit simulation. Its contract testing validates requests and responses with JSON Schema and exports OpenAPI 3.0, catching vendor breaking changes in CI; the chaos engine injects five fault types (latency, error, timeout, corruption, rate limiting) alongside a resilient client with retry, circuit breaking, and fallback. MockRecorder records real interactions and replays them as mock rules, with seamless GitHub Actions integration.
The Complete Guide to API Mocking for AI Applications: APIMock-AI in Practice
Building reliable AI applications depends on efficiently simulating LLM APIs, vector databases, and third-party services. APIMock-AI provides a production-grade set of mocking patterns for this scenario, covering contract testing, chaos engineering, and the full local development workflow.

Why AI Applications Especially Need API Mocking
AI applications depend on external APIs far more than traditional software: LLM inference endpoints, embedding services, vector databases, content moderation, tool calling… every external dependency is a potential point of failure in the CI pipeline.
Without mocks, development teams face: flaky CI, frequent rate limiting, expensive API bills, no offline development, slow feedback loops, and deep vendor lock-in. With mocks, these problems dissolve one by one—tests become reliable and controllable, development costs drop to zero, fully offline local workflows are supported, and feedback speeds up from minutes to seconds.
Mocking isn't a testing luxury—it's a necessity for AI development. The earlier you mock external dependencies, the faster you can ship and the easier you can debug.
Mock Server Architecture: Six Strategies Covering Every Scenario
At the core of APIMock-AI is a lightweight asynchronous mock server with six built-in response strategies: static responses, sequential responses, template rendering, dynamic callbacks, proxy forwarding, and chaos injection. Each rule includes a method, path pattern, priority, matching conditions, and hit count, with priority-based ordering and request logging.
Purpose-Built Mocks for LLM APIs
For mainstream endpoints like OpenAI's /v1/chat/completions and /v1/embeddings and Anthropic's /v1/messages, APIMock-AI offers ready-to-use builders. The embedding mock uses deterministic pseudo-vector generation based on an input hash, guaranteeing that the same input always returns the same vector, with L2 normalization applied—crucial for tests that need vector similarity computation.
Streaming responses get dedicated support too. By detecting the Accept: text/event-stream header, the mock server pushes content chunk by chunk in SSE format, fully simulating the streaming protocol that ends with data: [DONE]. Rate limit mocking is implemented based on client IP and a sliding window, returning standard 429 status codes and Retry-After headers.
SSE (Server-Sent Events) is a one-way streaming push protocol at the HTTP layer. The server declares the response type via Content-Type: text/event-stream, then sends event chunks one by one prefixed with data:, with each message separated by two newlines. The streaming completion interfaces of LLM providers like OpenAI and Anthropic are all built on SSE, with clients using EventSource or low-level fetch streaming to render tokens one at a time. When mocking a streaming interface, besides assembling data: chunks in SSE format, you must also fully simulate the end signal—OpenAI terminates with data: [DONE], while Anthropic ends with a message_stop event. If a mock omits the terminator, the client will keep waiting until the connection times out, and the symptom in tests is "hanging" rather than "erroring," making it very costly to diagnose. Moreover, SSE chunk boundaries do not represent message boundaries, so the mock server must ensure each event chunk is itself complete, parseable JSON to be correctly consumed by real clients.
Contract Testing: Making API Behavior Verifiable and Evolvable
Contract testing is the key capability that sets APIMock-AI apart from ordinary mocking tools. It uses a structured APIContract to describe each endpoint's parameters, request body schema, and response schema, and can be exported directly as an OpenAPI 3.0 specification.
During test execution, ContractTester sends requests to a real or mock service, then uses JSON Schema to validate whether the request and response bodies conform to the contract. This means: when a vendor API undergoes a breaking change, the test fails immediately with specific schema differences; when your client code mistakenly sends parameters that don't conform to the contract, it's caught too.
Taking OpenAI as an example, the contract defines the Authorization header as required, temperature ranging from 0-2, messages as an array type, and the response must contain the id/object/created/model/choices/usage fields. Any deviation is captured in CI.
JSON Schema is a specification for describing the structure of JSON data, declaring the shape and constraints of fields through keywords like type, properties, required, enum, minimum/maximum, and pattern. The core approach of contract testing is: write a JSON Schema for each of the vendor API's request and response bodies, then use a validator to check real traffic item by item. Compared to handwritten assertions, schemas have the advantages of being declarative, composable ($ref, allOf, oneOf), and capable of auto-generating documentation. OpenAPI 3.0 is a layer of interface description standard built on top of JSON Schema, additionally defining paths, methods, parameter locations, security schemes, and response codes, so contracts can be exported directly as OpenAPI documents and reused by Swagger UI, Postman, and code generators. Understanding the relationship between these two layers helps clarify the role of contract testing: Schema governs "what the data looks like," OpenAPI governs "how the interface is called," and breaking change detection is essentially comparing the schemas of two versions—removing required fields, narrowing enums, and changing types are all treated as incompatible.
Chaos Engineering: Validating Resilience in a Controlled Environment
What AI applications fear most in production isn't normal errors, but unexpected failures like timeouts, rate limiting, and corrupted responses. APIMock-AI's ChaosEngine provides five fault injection capabilities: latency injection (with configurable min/max millisecond ranges and trigger probability), error injection (returning 500/502/503/504 proportionally), timeout simulation, response corruption (returning invalid JSON), and rate limit injection.
Chaos experiments can be run on a schedule: specifying duration, target service, fault intensity, and test functions, the engine automatically tracks total requests, success rate, error rate, and P95 latency. These metrics can be used directly for quality gates in CI.
Resilience Patterns: Retry, Circuit Breaking, and Fallback
With chaos injection in place, you can validate whether your application's resilience strategies actually work. APIMock-AI's companion ResilientLLMClient implements a complete stack of resilience patterns:
Exponential backoff retry: max attempts, base delay, exponential factor, max delay cap, and jitter (50%-150% randomization) are all configurable.
Circuit breaker: after failures exceed a threshold, the breaker opens, and within the timeout window requests either take the fallback path directly or throw; after the window ends, it enters half-open state, probing with a small number of requests to see if the service has recovered.
Fallback degradation: provides three strategies—cache fallback (returning the last cached result), static fallback (returning a friendly message), and degraded fallback (switching to a smaller model, limiting token count, disabling streaming).
This combination lets teams confirm in a chaos environment that: latency injection doesn't cause request avalanches, error injection doesn't crash the whole service, and the circuit breaker kicks in at the right moment.
The Circuit Breaker pattern is borrowed from electrical engineering: when the failure rate exceeds a threshold, the switch "trips" into the Open state, and subsequent requests are no longer actually sent—they immediately fail or take a fallback path, avoiding continuously overwhelming a failing downstream and preventing caller threads from being tied up. After a cooldown window, it enters the Half-Open state, allowing a small number of probe requests through; if they succeed, it resets to Closed, and if they still fail, it returns to Open. It's complementary to, not a replacement for, retries: retries handle transient blips, circuit breakers handle sustained failures, and when combined, the circuit breaker should sit outside the retry layer—otherwise every retry hammers an already unavailable service. In production you also need to guard against flapping—too short a cooldown causes the breaker to open and close repeatedly—so it's often paired with exponential backoff and jitter to spread the retry requests of many clients across the time axis, preventing a thundering-herd-style synchronized reconnect from crushing a service that just recovered.
From Mock to Record-and-Replay: A Production-Grade Workflow
APIMock-AI's MockRecorder supports recording real API interactions and replaying them as mock rules. During recording, it captures the request method, path, headers, and body, along with the response status code, headers, and body. After stopping the recording, interactions are grouped by endpoint: a single interaction generates a static rule, while multiple interactions generate a sequential rule. This means you can first run a complete flow against the real API, then solidify the recorded data into a repeatable mock test suite.
CI/CD Integration: Making Mocks Part of the Pipeline
The GitHub Actions workflow demonstrates a complete integration approach: start the mock server, configure mock rules for each vendor, run contract tests, unit tests, and resilience tests in sequence, then generate and upload the test report. Chaos engineering runs independently as a scheduled task (e.g., every Monday at 6 a.m.), with Slack alerts on failure.
Local development has a corresponding launch script that spins up three instances simultaneously—LLM mock, vector database mock, and embedding service mock—and outputs their respective access addresses. Configuration files use YAML format, supporting definitions of sequential responses and callback functions.
Product Capabilities at a Glance
APIMock-AI's capability list includes: core mocking (static/sequential/template/callback/proxy/chaos), contract testing (OpenAPI 3.0, request validation, response validation, schema evolution detection, breaking change alerts), developer experience (zero-config startup, hot reload, request logging, validation API, VS Code extension, CLI tool), resilience testing (latency/error/timeout/corruption/rate limit/circuit breaker testing), integrations (OpenAI, Anthropic, Gemini, Qdrant, Pinecone, Weaviate, Milvus, GraphQL, gRPC), and deployment (Docker, Kubernetes Operator, Helm Chart, GitHub Action, GitLab CI).
Summary
The complexity of AI applications comes from the uncontrollability of external dependencies. APIMock-AI turns that uncontrollability into programmable mock rules: contract testing locks down API behavior boundaries, chaos engineering validates fault resilience, and record-and-replay solidifies real interactions—ultimately letting the entire AI ecosystem be fully simulated locally. For any team that depends on external LLMs and vector services, this is an engineering practice worth incorporating into the development workflow.
Related articles

Felix Rieseberg Personal Homepage
Felix Rieseberg's personal homepage sparks attention on Hacker News. This article covers the Slack engineer's technical background, the rise and fall of nativefier, and the value of a developer homepage as a tech calling card.

@ai-sdk/vue 3.0.306 Patch Release Syncs with [email protected] Update
@ai-sdk/vue 3.0.306 released, synced with [email protected]. A routine patch release for the Vercel AI SDK ecosystem with no public API changes; the Vue adapter layer stays version-aligned with the core package.

Gemini 4 Argon API Launches: The Era of Million-Token Output Begins
Google launches Gemini 4 Argon with sparse MoE architecture, up to 1M output tokens, and a 68.90% Vals Index score, available across API, AI Studio, and the Gemini app.