Chat API
Read this page as MarkdownIn this section: Requests and streaming
Requests and streaming
Learn how to send a Chat API request and receive a streaming or non-streaming response.
The request
A JSON object with model and messages. stream: true switches to server-sent events; stream_options: {"include_usage": true} is accepted. Other OpenAI fields (temperature, tools, response_format) are ignored: Miles decides how it answers.
curl https://askmiles.ai/v1/chat/completions \
-H "Authorization: Bearer $MILES_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "miles", "messages": [{"role": "user", "content": "Which card should I use at Costco?"}]}'With the SDK
from openai import OpenAI
client = OpenAI(base_url="https://askmiles.ai/v1", api_key=MILES_API_KEY)
response = client.chat.completions.create(
model="miles",
messages=[{"role": "user", "content": "Which card should I use at Costco?"}],
)
print(response.choices[0].message.content)Set the key from your environment. A key is shown once at mint and never again.
Streaming
Set stream: true in the same request body. The response uses text/event-stream, with one OpenAI chat completion chunk per data: line. It ends with data: [DONE].
: keepalive
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1762588800,"model":"miles","choices":[{"index":0,"delta":{"role":"assistant","content":"At a Costco warehouse"},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1762588800,"model":"miles","choices":[{"index":0,"delta":{"content":", use your"},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","created":1762588800,"model":"miles","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]from openai import OpenAI
client = OpenAI(base_url="https://askmiles.ai/v1", api_key=MILES_API_KEY)
with client.chat.completions.create(
model="miles",
messages=[{"role": "user", "content": "Is the Sapphire Reserve worth keeping?"}],
stream=True,
) as stream:
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)Miles starts streaming after classification, tool calls, and reasoning. The stream sends no data: event until the answer starts, which can take tens of seconds, longer with tool rounds. Until then the stream sends a : keepalive SSE comment after each 15 seconds without a frame, which OpenAI clients skip. An error after the stream opens arrives as one final data: event with the same error envelope as a non-streaming response. The stream then closes with no [DONE].
Follow-ups
Miles has no conversation state. Include earlier turns as assistant and user messages before the new question to ask a follow-up. Miles reads the new question and the last 26 earlier user and assistant messages.
Rate limits
Successful, streaming, and 429 responses include rate-limit headers. See Rate limits.