Chat completions
POST /v1/chat/completions is an OpenAI-compatible chat endpoint
backed by HKM LLM 1 (hakim-chat-v1), our Arabic-first LLM.
Drop-in for any code that targets the OpenAI Chat Completions
reference · swap the base URL and key, and your existing
openai-python / openai-node integration keeps working without
a rewrite.
The canonical contract (request shape, every parameter, every
finish reason) lives at
api.tryhakim.ai/docs (interactive
Scalar reference) and
api.tryhakim.ai/openapi.json
(machine-readable). When this guide and the OpenAPI disagree, the
OpenAPI wins.
Quick start
curl https://api.tryhakim.ai/v1/chat/completions \
-H "Authorization: Bearer $HAKIM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "hakim-chat-v1",
"messages": [
{"role": "system", "content": "أنت مساعد عربي مفيد."},
{"role": "user", "content": "اكتب قصيدة قصيرة عن البحر."}
],
"temperature": 0.7
}'
The response is OpenAI-shaped:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1748400000,
"model": "hakim-chat-v1",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "..." },
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 32, "completion_tokens": 128, "total_tokens": 160 },
"hakim_usage": {
"request_id": "req_…",
"kind": "llm_chat",
"units": 160,
"unit_type": "tokens",
"credits": 160,
"cost_usd": "0.00",
"model": "hakim-chat-v1",
"billing_period_start": "2026-05-01T00:00:00Z",
"billing_period_end": "2026-06-01T00:00:00Z"
}
}
The hakim_usage block on the body mirrors the x-hakim-usage-*
response headers · same fields, same shape · so logs that strip
custom headers still capture the per-request cost. See the
usage reporting guide for the
warehouse recipe.
Streaming (SSE)
Set "stream": true and the server replies with
Content-Type: text/event-stream. Each event carries one
data: {…} frame containing a ChatCompletionChunk. The final
chunk before data: [DONE] carries the rolled-up usage block.
curl -N https://api.tryhakim.ai/v1/chat/completions \
-H "Authorization: Bearer $HAKIM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "hakim-chat-v1",
"messages": [{"role": "user", "content": "مرحبا!"}],
"stream": true
}'
The wire is OpenAI's:
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0,"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"delta":{"content":"مرحبا"},"index":0,"finish_reason":null}]}
…
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"delta":{},"index":0,"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":12,"total_tokens":15}}
data: [DONE]
The -N flag disables curl's response buffering so chunks
render as they arrive.
Observability headers on streaming. Because the response stream is hijacked before the upstream finishes, only the upfront-knowable headers are stamped on streaming responses:
x-hakim-usage-kind,x-hakim-usage-unit-type,x-hakim-model, plus the plan / period / credits snapshot. The per-requestunits/credits/cost_usdride the terminal SSE chunk'susageblock · OpenAI convention · and are not echoed as headers. The Node + Python SDKs surface both: ausage_preflightshape from the headers, and the rolled-upusagefrom the final chunk.
Reasoning / chain-of-thought
hakim-chat-v1 is a thinking-capable model. By default the model
runs without the chain-of-thought (CoT) trace: enabling it
adds 10–50× latency for short prompts and burns completion tokens
on text that doesn't reach the user's screen in time.
The contract is symmetric and explicit:
| Mode | Default | Override | Wire effect |
|---|---|---|---|
| Non-streaming | off | "reasoning": {"enabled": true} | Response carries choices[].message.reasoning (the CoT) and choices[].message.content (the final answer) as separate fields. |
| Streaming | off | not allowed | Request schema rejects stream=true + reasoning.enabled=true with a 400 invalid_request_error. Make a non-stream call if you need CoT. |
Opt-in example:
curl https://api.tryhakim.ai/v1/chat/completions \
-H "Authorization: Bearer $HAKIM_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "hakim-chat-v1",
"messages": [{"role": "user", "content": "Solve 24 × 17 step by step."}],
"reasoning": {"enabled": true}
}'
The response carries both fields:
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"reasoning": "I need to compute 24 × 17. Break it down: 24 × 10 = 240, 24 × 7 = 168, total 408.",
"content": "24 × 17 = 408."
},
"finish_reason": "stop"
}
]
}
message.reasoning and message.content are distinct fields
— do not concatenate them or display them in the same UI element.
The reasoning trace is meant for debugging, evaluation, and
showing your work; the final answer is what reaches the end user.
SDKs
Node.js
The @tryhakim/voice SDK exposes the namespace as
hakim.chat.completions:
import { Hakim } from '@tryhakim/voice';
const hakim = new Hakim({ apiKey: process.env.HAKIM_API_KEY });
// Non-streaming.
const completion = await hakim.chat.completions.create({
model: 'hakim-chat-v1',
messages: [
{ role: 'system', content: 'أنت مساعد عربي مفيد.' },
{ role: 'user', content: 'اكتب قصيدة قصيرة عن البحر.' },
],
temperature: 0.7,
});
console.log(completion.choices[0].message.content);
console.log('billed', completion.usage_headers?.credits, 'credits');
// Streaming.
const { stream } = await hakim.chat.completions.stream({
model: 'hakim-chat-v1',
messages: [{ role: 'user', content: 'مرحبا!' }],
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta.content;
if (delta) process.stdout.write(delta);
if (chunk.usage) console.log('\n[usage]', chunk.usage);
}
Python
from hakim import AsyncHakim
async with AsyncHakim(api_key="hk_…") as hakim:
completion = await hakim.chat.completions.create(
model="hakim-chat-v1",
messages=[
{"role": "system", "content": "أنت مساعد عربي مفيد."},
{"role": "user", "content": "اكتب قصيدة قصيرة عن البحر."},
],
temperature=0.7,
)
print(completion["choices"][0]["message"]["content"])
stream = await hakim.chat.completions.stream(
model="hakim-chat-v1",
messages=[{"role": "user", "content": "مرحبا!"}],
)
async for chunk in stream:
delta = chunk["choices"][0]["delta"]
if "content" in delta:
print(delta["content"], end="", flush=True)
OpenAI SDKs
Because the wire is OpenAI-compatible, the official openai
package works too · point it at Hakim's base URL:
from openai import OpenAI
client = OpenAI(
base_url="https://api.tryhakim.ai/v1",
api_key="hk_…",
)
response = client.chat.completions.create(
model="hakim-chat-v1",
messages=[{"role": "user", "content": "مرحبا!"}],
)
You give up the typed hakim_usage block on the response and the
usage_headers decoded shape · both are Hakim-specific · but the
core completion flow is identical.
Models
| Public id | Marketing alias | Released | Notes |
|---|---|---|---|
hakim-chat-v1 | hkm-llm-1 | ✅ | Arabic-first chat. Streaming SSE. CoT opt-in. |
Pass either id on the wire · the marketing alias canonicalises to the public id server-side. New tiers will append additional rows here without re-numbering existing ones.
Pricing & billing
Chat completions are token-denominated: one unit = one token
(input + output, mirrored from the upstream's usage.total_tokens).
The standard observability stack applies · the
usage reporting guide covers the
header set, the JSON usage block, and the GET /v1/limits /
GET /v1/usage/events/:id endpoints that surface the same rows.
For chat specifically:
x-hakim-usage-kind: llm_chatx-hakim-usage-unit-type: tokensx-hakim-usage-units=prompt_tokens + completion_tokens(theUsageEvent.metadatarow carries the split asinput_tokens/output_tokensfor downstream attribution).
Errors
Standard ApiError envelope (errors guide).
Chat-specific 400 codes worth knowing:
| Code | Meaning |
|---|---|
messages_too_long | Aggregate messages[].content exceeded 256 KB. |
model_unknown | Model id is not a public canonical id or a registered alias. |
tool_role_not_supported | messages[].role === "tool" · reserved for the function-calling rollout (P7). |
reasoning_stream_conflict | stream=true plus reasoning.enabled=true. See the reasoning policy above. |
Limits
- Max messages per request: 200.
- Max aggregate content size: 256 KB across every text part.
- Parallel completions:
n=1only in v1. The schema accepts the field so SDKs that always send it don't 400, but rejectsn > 1. - Tool calls: schema-accepted but stripped before upstream dispatch. Public tool-calling support lands in P7.