Anthropic Claude Messages API

The POST /v1/messages endpoint implements the Anthropic Messages API with streaming SSE using event: + data: format, including content blocks for text and tool use.

Endpoint

Method Path Format
POST /v1/messages SSE (event: + data:) or JSON

Unit Test: Text Streaming

claude-text.test.ts ts
const textFixture = {
  match: { userMessage: "hello" },
  response: { content: "Hi there!" },
};

const instance = await createServer([textFixture]);

const res = await post(`${instance.url}/v1/messages`, {
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "hello" }],
  stream: true,
});

const events = parseClaudeSSEEvents(res.body);
const types = events.map(e => e.type);

expect(types).toContain("message_start");
expect(types).toContain("content_block_start");
expect(types).toContain("content_block_delta");
expect(types).toContain("message_stop");

Unit Test: Tool Use

claude-tools.test.ts ts
const toolFixture = {
  match: { userMessage: "weather" },
  response: {
    toolCalls: [{ name: "get_weather", arguments: '{"city":"NYC"}' }]
  },
};

const instance = await createServer([toolFixture]);

const res = await post(`${instance.url}/v1/messages`, {
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "what is the weather?" }],
  stream: true,
});

const events = parseClaudeSSEEvents(res.body);
const blockStart = events.find(
  e => e.type === "content_block_start"
    && e.content_block?.type === "tool_use"
);
expect(blockStart.content_block.name).toBe("get_weather");

SSE Event Sequence

Claude Messages streaming produces these events:

  1. message_start — message metadata (id, model, role, usage)
  2. content_block_start — text or tool_use block
  3. content_block_delta — text_delta or input_json_delta
  4. content_block_stop
  5. message_delta — stop_reason, usage
  6. message_stop

Request Translation

llmock internally translates Anthropic requests to a unified format for fixture matching. The claudeToCompletionRequest() function handles mapping Anthropic message arrays (including content block arrays) to OpenAI-style messages so the same fixtures work across all providers.