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

# Model Request

> Create chat completions with Maitai metadata (Application, Intent, Session).

export const CompletionUsageHeadline = 'Usage statistics for the completion request.';

export const StreamUsageHeadline = <p>
        An optional field that will only be present when you set <code>stream_options</code> with <code>include_usage: true</code> in your
        request. When present, it contains a null value except for the last chunk which contains the token usage
        statistics for the entire request.
    </p>;

export const UsageSnippet = ({headline}) => <ResponseField name="usage" type="object">
        {headline}
        <Expandable title="properties">
            <ResponseField name="completion_tokens" type="integer">
                Number of tokens in the generated completion.
            </ResponseField>
            <ResponseField name="prompt_tokens" type="integer">
                Number of tokens in the prompt.
            </ResponseField>
            <ResponseField name="total_tokens" type="integer">
                Total number of tokens used in the request (prompt + completion).
            </ResponseField>
        </Expandable>
    </ResponseField>;

## Create chat completion

<RequestExample>
  ```python Python (async) theme={null}
  import maitai

  client = maitai.MaitaiAsync()

  messages = [
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "Say hello in one sentence."},
  ]

  response = await client.chat.completions.create(
      messages=messages,
  application="demo_app",
  intent="GREETING",
      session_id="YOUR_SESSION_ID",
  # model="llama3-70b-8192",  # Optional: omit to use Portal config
  )
  ```

  ```python Python (sync) theme={null}
  import maitai

  client = maitai.Maitai()

  messages = [
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "Say hello in one sentence."},
  ]

  response = client.chat.completions.create(
      messages=messages,
  application="demo_app",
  intent="GREETING",
      session_id="YOUR_SESSION_ID",
  # model="llama3-70b-8192",  # Optional: omit to use Portal config
  )
  ```

  ```javascript Node theme={null}
  import Maitai from "maitai";

  const maitai = new Maitai();

  const messages = [
  { role: "system", content: "You are a helpful assistant." },
  { role: "user", content: "Say hello in one sentence." },
  ];

  const response = await maitai.chat.completions.create({
      messages: messages,
  application: "demo_app",
  intent: "GREETING",
      session_id: "YOUR_SESSION_ID",
  // model: "llama3-70b-8192", // Optional: omit to use Portal config
  });
  ```
</RequestExample>

## Image input

<RequestExample>
  ```python Python (sync) theme={null}
  import maitai

  client = maitai.Maitai()

  messages = [
      {
          "role": "user",
          "content": [
              {"type": "input_text", "text": "what's in this image?"},
              {
                  "type": "input_image",
                  "image_url": "https://upload.wikimedia.org/wikipedia/commons/0/05/Mt_Rainier_NOAA_2000.jpg",
              },
          ],
      }
  ]

  response = client.chat.completions.create(
      messages=messages,
      application="demo_app",
      intent="VISION",
      session_id="YOUR_SESSION_ID",
      model="gpt-4o-mini",
      evaluation_enabled=False,
      apply_corrections=False,
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node theme={null}
  import Maitai from "maitai";

  const maitai = new Maitai();

  const messages = [
    {
      role: "user",
      content: [
        { type: "input_text", text: "what's in this image?" },
        {
          type: "input_image",
          image_url:
            "https://upload.wikimedia.org/wikipedia/commons/0/05/Mt_Rainier_NOAA_2000.jpg",
        },
      ],
    },
  ];

  const response = await maitai.chat.completions.create({
    messages,
    application: "demo_app",
    intent: "VISION",
    session_id: "YOUR_SESSION_ID",
    model: "gpt-4o-mini",
    evaluation_enabled: false,
    apply_corrections: false,
  });

  console.log(response.choices[0].message.content);
  ```
</RequestExample>

## Maitai parameters

<ParamField path="application" type="string" required>
  The Application reference name (shown in the Portal). Maitai uses this to group your traffic.
</ParamField>

<ParamField path="intent" type="string" required>
  The Intent / action type for this request (also called `action_type`). This is used for organization, configuration, and quality tooling.
</ParamField>

<ParamField path="session_id" type="string">
  Optional but recommended. Groups related requests into a session in the Portal. If omitted, the SDK will generate one.
</ParamField>

<ParamField path="metadata" type="map<string, any>">
  Optional metadata tags stored with the request for filtering/debugging.
</ParamField>

<ParamField path="reasoning" type="object">
  Provider-agnostic reasoning controls: `effort` (`"low"` | `"medium"` | `"high"`, optional) and `summary` (boolean, default `false`) to include thinking text on the response. See [Reasoning](/sdk/reasoning).
</ParamField>

<ParamField path="reasoning_effort" type="string">
  <Warning>
    **Deprecated.** Use the `reasoning` object with an `effort` field (for example `reasoning={"effort": "high"}` in Python or `reasoning: { effort: "high" }` in Node) instead. See [Reasoning](/sdk/reasoning). This field still works for backward compatibility.
  </Warning>
</ParamField>

## Evaluations and corrections (optional)

<ParamField path="evaluation_enabled" type="boolean">
  Enable/disable Sentinel evaluations for this request (Portal config also controls this).
</ParamField>

<ParamField path="apply_corrections" type="boolean">
  When enabled, Maitai may apply an automatic correction if evaluations find a fault.
  <Warning>Corrections require server-side inference.</Warning>
</ParamField>

<ParamField path="safe_mode" type="boolean">
  Prioritize accuracy over latency when applying corrections.
</ParamField>

<ParamField path="callback" type="function(EvaluateResponse)">
  Optional callback invoked with the evaluation results. See [Evaluate](/sdk/evaluate).
</ParamField>

## Inference routing

<ParamField path="server_side_inference" type="boolean">
  Controls where inference runs.

  <Expandable title="notes">
    <p>
      * <b>true</b>: inference is routed through Maitai (recommended). This enables server-side features like corrections and `input_safety_score`.
    </p>

    <p>
      * <b>false</b>: run inference with your own client. To send those request/response pairs to Maitai for monitoring and indexing, use <a href="/sdk/indexing">maitai.log() (bring your own inference)</a>.
    </p>
  </Expandable>
</ParamField>

## Fallbacks (optional)

<ParamField path="fallback_config" type="object">
  Optional fallback configuration used when the primary model fails or times out.

  <Expandable title="properties">
    <ParamField path="strategy" type="string" default="reactive">
      Must be one of `reactive`, `first_response`, or `timeout`.
    </ParamField>

    <ParamField path="model" type="string">
      The fallback model name.
    </ParamField>

    <ParamField path="timeout" type="number">
      Timeout (seconds) for strategies that use a timeout.
    </ParamField>
  </Expandable>
</ParamField>

## OpenAI parameters

Maitai accepts the standard OpenAI `chat.completions.create` parameters (for example: `model`, `temperature`, `tools`, `tool_choice`, `response_format`, `stream`, etc.). If you set them in the request, they override Portal defaults.

Related guides:

* [Structured Output](/sdk/structured_output)
* [Tool Calling](/sdk/tool_calling)

## Response fields

Maitai returns an OpenAI-compatible `chat.completion` response, plus a few extra fields:

<ResponseField name="request_id" type="string">
  The Maitai request identifier.
</ResponseField>

<ResponseField name="evaluate_response" type="object or null">
  Evaluation results (when evaluations are enabled).
</ResponseField>

<ResponseField name="correction_applied" type="boolean">
  Whether a correction was applied.
</ResponseField>

<ResponseField name="input_safety_score" type="number or null">
  Optional input safety score (when enabled). A number between 0 and 1 where lower values indicate a potential jailbreak attempt.
</ResponseField>

<ResponseField name="first_token_time" type="number">
  Time to first token (milliseconds), when available.
</ResponseField>

<ResponseField name="response_time" type="number">
  Total response time (milliseconds), when available.
</ResponseField>

<ResponseField name="fallback_reason" type="string or null">
  Present when a fallback model was used (explains why).
</ResponseField>

<UsageSnippet headline={CompletionUsageHeadline} />

## Streaming usage

<UsageSnippet headline={StreamUsageHeadline} />
