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

# Chat completions

> POST /v1/chat/completions — request fields, response, streaming and tool calls.

OpenAI-compatible chat endpoint for every text model. Existing OpenAI SDK code works by changing the base URL and the model ID.

```text theme={null}
POST https://refillapi.com/v1/chat/completions
```

## Request fields

| Field | Type | Description |
| - | - | - |
| `model` | string | Required. Exact model ID, e.g. `deepseek-v4-pro` |
| `messages` | array | Required. Conversation messages; `role` is `system`, `user`, `assistant`, or `tool` for tool results |
| `max_tokens` | integer | Optional. Maximum output tokens, **including reasoning**. Too small a value can leave the answer empty — see `finish_reason` |
| `stream` | boolean | Optional. `true` streams the answer as server-sent events |
| `tools` | array | Optional. Functions the model may call |
| `prompt_cache_key` | string | Optional. Send the same string for every request of one conversation to improve cache hits on models with cached-input pricing |

Other OpenAI parameters (`temperature`, `top_p`, `response_format`, …) are passed through where the model supports them. Start with a minimal request and add parameters one at a time.

## Example

```bash curl theme={null}
curl https://refillapi.com/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-pro",
    "messages": [
      {"role": "system", "content": "Answer in one sentence."},
      {"role": "user", "content": "Why is the sky blue?"}
    ],
    "max_tokens": 1024
  }'
```

```python Python theme={null}
from openai import OpenAI

client = OpenAI(api_key="YOUR_API_KEY", base_url="https://refillapi.com/v1")
response = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[
        {"role": "system", "content": "Answer in one sentence."},
        {"role": "user", "content": "Why is the sky blue?"},
    ],
    max_tokens=1024,
)
print(response.choices[0].message.content)
```

## Response

```json theme={null}
{
  "id": "e85dc74d-c71e-4e95-a112-5baefd539b69",
  "object": "chat.completion",
  "model": "deepseek-v4-pro",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Air molecules scatter short blue wavelengths of sunlight much more than red ones.",
        "reasoning_content": "The user asks why the sky is blue, in one sentence..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 91,
    "completion_tokens": 80,
    "total_tokens": 171,
    "prompt_tokens_details": { "cached_tokens": 0 },
    "completion_tokens_details": { "reasoning_tokens": 59 }
  }
}
```

| Field | Description |
| - | - |
| `choices[0].message.content` | The answer |
| `choices[0].message.reasoning_content` | Reasoning of thinking models. For display or debugging; do not send it back in the next turn |
| `choices[0].message.tool_calls` | Functions the model wants to call |
| `usage.completion_tokens` | Output tokens, **including reasoning** |
| `usage.prompt_tokens_details.cached_tokens` | Input tokens served from cache, if the model supports caching |

Models may add their own fields. Read the ones you need and do not depend on field order.

### `finish_reason`

| Value | Meaning | What to do |
| - | - | - |
| `stop` | Finished normally | Read `content` |
| `length` | Hit `max_tokens` | Raise `max_tokens`. With a small limit, reasoning models can spend it all on thinking and return empty `content` |
| `tool_calls` | The model wants to call a function | Run it and send the result back (below) |

## Streaming

Set `"stream": true`. The answer arrives as server-sent events; join the `delta.content` of each chunk and skip chunks without text.

```python theme={null}
stream = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "Write a haiku about rain."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

With cURL, add `-N` to turn off output buffering. Use streaming for long answers: a non-streaming request that runs longer than about 5 minutes can time out with 504.

## Tool calls

1. Send `tools` with the request:

```json theme={null}
{
  "model": "deepseek-v4-pro",
  "messages": [{ "role": "user", "content": "What's the weather in Paris?" }],
  "tools": [{
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get the current weather for a city",
      "parameters": {
        "type": "object",
        "properties": { "city": { "type": "string" } },
        "required": ["city"]
      }
    }
  }]
}
```

2. The model answers with `finish_reason: "tool_calls"` and a `tool_calls` list. `function.arguments` is a JSON **string**; parse it before calling your function.

3. Append the assistant message and one `role: "tool"` message per call, with the matching `tool_call_id`, then send the request again with the same `tools`:

```json theme={null}
{
  "role": "tool",
  "tool_call_id": "call_00_ELhHebn204OKGIUlJnQy5964",
  "content": "{\"weather\": \"sunny\", \"temp_c\": 22}"
}
```

## Conversations

The API does not remember earlier requests. Send the history you want the model to see in `messages` every time; history counts as input tokens.

## Anthropic format

Text models are also available on the Anthropic Messages endpoint, `POST https://refillapi.com/v1/messages`, for tools built on the Anthropic SDK such as [Claude Code](https://docs.refillapi.com/integrations/claude-code.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.