Loading
Loading
POST /v1/chat/completions — chat models, streaming, tools and image input.
The body is the same as in OpenAI Chat Completions: model, messages, stream, temperature, tools and other fields. For OpenAI-compatible providers (OpenAI, DeepSeek, xAI), the body is passed through almost unchanged; for Claude and Gemini, the request and response are translated to and from the OpenAI format.
from openai import OpenAIimport os client = OpenAI( base_url="https://llmproxy.org/v1", # the only difference from OpenAI api_key=os.environ["MAXAI_API_KEY"],) resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "Explain what an API gateway is in two sentences."}], stream=True,)for chunk in resp: print(chunk.choices[0].delta.content or "", end="")
Only models of type chat work here. Any other type returns 400.
With stream: true, the response arrives as OpenAI-format SSE chunks ending with data: [DONE]. A chunk with usage is sent if you pass stream_options: {include_usage: true}, as with OpenAI. Reasoning models send their reasoning in delta.reasoning_content.
In a stream, the charged amount is an SSE comment : maxai-charged-cents <number> after the last frame. SDKs ignore comments, so it doesn’t break anything.
If you don’t pass max_tokens, a cap of 4096 output tokens applies, and the same amount is held on your balance. Reasoning models (GPT-5 and GPT-6, DeepSeek Thinking and Pro) may need more for a long answer: pass your own max_completion_tokens.
For OpenAI models you can also send max_tokens: it’s renamed to max_completion_tokens for you. Values above the model’s limit are capped at its maximum.
tools and tool_choice work with models that support them. They’re passed as is to OpenAI-compatible providers; for Claude and Gemini they’re translated to the native format, and tool_calls come back in the OpenAI format. Send the tool result as a message with the tool role.
tools = [{ "type": "function", "function": { "name": "get_weather", "description": "Current weather in a city", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, },}] resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "What is the weather in Lisbon?"}], tools=tools,)call = resp.choices[0].message.tool_calls[0]print(call.function.name, call.function.arguments)
response_format (json_object, json_schema) is passed unchanged to OpenAI-compatible providers. For Claude and Gemini this field isn’t translated: ask for JSON in the prompt and validate the response on your side.
resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{"role": "user", "content": "Return JSON with the city and country fields for Paris."}], response_format={"type": "json_object"},)print(resp.choices[0].message.content)
Add an image_url part to the message content: an https:// link or data:image/…;base64,…. The model must support image input. Each image adds about 2,000 input tokens to the hold on your balance; you’re charged for actual usage.
resp = client.chat.completions.create( model="mistralai/mistral-nemo", messages=[{ "role": "user", "content": [ {"type": "text", "text": "What is in this image?"}, {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}, ], }],)
A non-streaming response includes usage with token counts, and the charged amount is in the x-maxai-charged-cents and x-maxai-charged-usd headers. If the provider doesn’t return usage, the charge is based on a token estimate, not zero.