> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mrdn.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat Completion

> Paid chat completion, streamed as server-sent events

Runs one chat completion against the selected Dolphin model. This endpoint is x402-protected: unpaid requests receive `402 Payment Required`, and paid requests must include the `X-PAYMENT` header.

## Headers

<ParamField header="Content-Type" type="string" required>
  Must be `application/json`.
</ParamField>

<ParamField header="X-PAYMENT" type="string">
  Base64-encoded signed x402 payment payload. Omit on the first request to
  receive the payment requirements via a 402 response.
</ParamField>

## Body

<ParamField body="messages" type="array" required>
  Conversation history as an array of `{ role, content }` objects. Roles are
  `user` and `assistant`. Must be non-empty.
</ParamField>

<ParamField body="model" type="string" default="dolphinserver:24B">
  Model ID. Must be one of the IDs returned by
  [`GET /v1/inference/models`](/api-reference/inference/models). Invalid values return
  `400` with the supported list.
</ParamField>

<ParamField body="template" type="string" default="logical">
  Prompt template. Must be one of the names returned by
  [`GET /v1/inference/templates`](/api-reference/inference/templates). Invalid values
  return `400` with the supported list.
</ParamField>

## Response

On success (`200`), the completion is streamed as `text/event-stream`. Each event is an OpenAI-style chunk; concatenate `choices[0].delta.content` across chunks to build the full reply. Settlement details (including the transaction hash) are returned in the `X-PAYMENT-RESPONSE` header.

<RequestExample>
  ```bash Unpaid (get quote) theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
  curl -X POST https://api.mrdn.finance/v1/inference/chat \
    -H "Content-Type: application/json" \
    -d '{
      "messages": [{ "role": "user", "content": "hey" }],
      "model": "dolphinserver:24B",
      "template": "logical"
    }'
  ```

  ```typescript x402-fetch (auto-pays) theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
  import { privateKeyToAccount } from "viem/accounts";
  import { wrapFetchWithPayment } from "@meridian/x402-fetch";

  const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`);
  const fetchWithPayment = wrapFetchWithPayment(fetch, account);

  const response = await fetchWithPayment("https://api.mrdn.finance/v1/inference/chat", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      messages: [{ role: "user", content: "hey" }],
      model: "dolphinserver:24B",
      template: "logical",
    }),
  });

  for await (const chunk of response.body) {
    process.stdout.write(new TextDecoder().decode(chunk));
  }
  ```
</RequestExample>

<ResponseExample>
  ```json 402 Payment Required theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
  {
    "x402Version": 1,
    "error": {},
    "accepts": [
      {
        "scheme": "exact",
        "network": "base",
        "maxAmountRequired": "10000",
        "resource": "https://api.mrdn.finance/v1/inference/chat",
        "description": "One chat completion from Dolphin AI",
        "mimeType": "text/event-stream",
        "payTo": "0x8E7769D440b3460b92159Dd9C6D17302b036e2d6",
        "maxTimeoutSeconds": 60,
        "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
        "extra": { "name": "USD Coin", "version": "2" }
      }
    ]
  }
  ```

  ```text 200 OK (SSE stream) theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
  data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"24B","choices":[{"index":0,"delta":{"role":"assistant","content":"Hey"},"finish_reason":null}]}

  data: {"id":"chatcmpl-...","object":"chat.completion.chunk","model":"24B","choices":[{"index":0,"delta":{"content":" there!"},"finish_reason":null}]}

  data: [DONE]
  ```

  ```json 400 Bad Request theme={"theme":{"light":"github-light","dark":"github-dark-high-contrast"}}
  {
    "error": "Unknown model \"gpt-4\"",
    "supported": ["dolphinserver:24B", "dolphinserver2:6b"]
  }
  ```
</ResponseExample>
