Skip to content

Mock an LLM API

Before you begin

Role
Editor or higher.
You need
  • A project

An AI model response answers in the shape of a model provider’s API, so your application’s own SDK reads the message and tool calls you wrote, as a stream when it asks for one.

API shape format Path an SDK calls
OpenAI chat completions openai-chat /v1/chat/completions
OpenAI responses openai-responses /v1/responses
Anthropic messages anthropic-messages /v1/messages

The quickest start is a starter pack. On the project’s Mocks tab, open the arrow beside New mock and select New AI API endpoints. Choose OpenAI-compatible or Anthropic, select Preview, read the mocks it adds, and select Add endpoints. The mocks are added beside the project’s own, and nothing is replaced. A mock whose name is already in the project is left as it is, so adding a starter twice changes nothing. Start from a pack says which plans include packs.

To add one response by hand:

  1. Create a mock for POST and the path in the table, such as /v1/chat/completions.

  2. Open Response and select An AI model response, or on an existing response select AI model under Output.

  3. Choose the API shape, and write the Message. It is a template like any body, so You asked: {{jsonPath request.body '$.messages[0].content'}} echoes the question.

  4. For a tool call, select Add a tool call, and enter the tool’s name and its arguments as a JSON object, such as {"city": "Paris"}.

  5. Set Time to first token (ms) and Tokens a second for how fast the answer arrives, and select Save mock.

In the configuration document, the response holds ai instead of a body:

{
"statusCode": 200,
"ai": {
"format": "openai-chat",
"message": "The order ships on Friday.",
"toolCalls": [{ "name": "get_order", "arguments": "{\"id\": \"42\"}" }],
"firstTokenMs": 400,
"tokensPerSecond": 40
}
}

stopReason (end, length, tool_calls, content_filter), model and usage (inputTokens and outputTokens) are optional: left out, the stop reason follows from the tool calls, the model is the request’s, and the token counts are estimated from the text. cutAfter ends a stream after that many events.

Set the SDK’s base URL to the project’s address. Any API key is accepted unless the project is private.

import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: 'any', baseURL: 'https://orders-api.mockhub.dev/v1' });
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({ apiKey: 'any', baseURL: 'https://orders-api.mockhub.dev' });

A request whose JSON body says "stream": true gets the provider’s event stream, paced at the speed you set; any other request gets one JSON body, after the time the stream would have taken.

An agent asks the model, runs the tool it names, and asks again. Give the mock two responses, a tool call and then the answer, and return them as a sequence that keeps answering the last. The OpenAI-compatible starter’s Agent loop mock is an example.

To test failures, add responses that answer as the provider does when it refuses, a 401 with its error body, or a 429 with Retry-After, and ask for one by name with X-MockHub-Response. A stream that stops part way is cutAfter.

The SDK reads the message, tool calls and stop reason as it would a real model’s. Try it shows the message and tool calls, and Ask for a stream sets "stream": true in its request to show the events. The request log keeps the answer as sent, and opens a stream as Assembled text.

  • It is a shape, not a model: the answer is the text the mock holds, rendered as a template. Nothing reads the conversation, counts real tokens or keeps a session.
  • Token counts are estimates unless you set usage.
  • Reasoning, images, audio, Gemini and realtime APIs are not answered in their shapes. The OpenAI-compatible starter answers /v1/embeddings and /v1/models as plain JSON bodies.
  • An AI model response answers a 2xx status, and has no body, Content-Type, delay or stream of its own. Write a failure as an ordinary response.
  • Streams follow the plan’s stream limit, and arrive in one piece when they cannot be paced.

Report a problem with this page