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

# Create a chat completion

> Route an OpenAI Chat Completions request. An agent ID and session ID are required, as
headers (`X-Slng-Agent-Id`, `X-Slng-Session-Id`) or body fields (`slng_agent_id`,
`slng_session_id`); the body field wins if both are sent.




## OpenAPI

````yaml /api-reference/context-router/context-router.oas.yaml post /chat/completions
openapi: 3.1.0
info:
  title: Context Router API
  version: 1.0.0
  summary: >-
    OpenAI-compatible chat endpoints with routing, response caching and per-call
    templating.
  description: >
    Routes OpenAI-compatible requests to a configured model and returns the
    standard OpenAI

    response. Two endpoints share the same auth, routing, caching and
    templating:

      * `POST /chat/completions` - Chat Completions protocol. Recommended.
      * `POST /responses` - Responses protocol.

    SLNG request extensions: `template_variables`, `slng_config`,
    `slng_agent_id`,

    `slng_session_id`, `slng_analytics`, `slng_pure_proxy`. Response headers:

    `x-slng-request-id`, `x-slng-response-source`, `x-slng-model`.
servers:
  - url: https://{region}.context-router.slng.ai/v1
    description: Regional base URL. Pick the region closest to your callers.
    variables:
      region:
        default: india
        enum:
          - india
          - us
          - indonesia
        description: Deployment region. Other regions may exist.
security:
  - SlngApiKey: []
tags:
  - name: Chat
    description: OpenAI Chat Completions surface.
  - name: Responses
    description: OpenAI Responses-compatible endpoint.
paths:
  /chat/completions:
    post:
      tags:
        - Chat
      summary: Create a chat completion
      description: >
        Route an OpenAI Chat Completions request. An agent ID and session ID are
        required, as

        headers (`X-Slng-Agent-Id`, `X-Slng-Session-Id`) or body fields
        (`slng_agent_id`,

        `slng_session_id`); the body field wins if both are sent.
      operationId: createChatCompletion
      parameters:
        - $ref: '#/components/parameters/XSlngAgentId'
        - $ref: '#/components/parameters/XSlngSessionId'
        - $ref: '#/components/parameters/XSlngRequestId'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ChatCompletionRequest'
            examples:
              minimal:
                summary: Minimal routed call
                value:
                  model: slng/auto
                  messages:
                    - role: system
                      content: You are a helpful clinic assistant.
                    - role: user
                      content: What are your opening hours?
              templated:
                summary: Per-call personalization via template_variables
                value:
                  model: slng/auto
                  messages:
                    - role: system
                      content: >-
                        You are Sarah, a scheduling assistant for
                        {{clinic_name}}.
                    - role: user
                      content: hi, can you confirm my appointment?
                  temperature: 0.3
                  max_tokens: 256
                  template_variables:
                    clinic_name: Sunrise Clinic
              streaming:
                summary: Streaming with the usage trailer
                value:
                  model: slng/auto
                  messages:
                    - role: user
                      content: Hello
                  stream: true
                  stream_options:
                    include_usage: true
              inlineConfig:
                summary: Bring your own model configuration inline
                value:
                  model: slng/auto
                  messages:
                    - role: user
                      content: Hello
                  slng_config:
                    tiers:
                      '1':
                        - model: gpt-4o-mini
                          weight: 100
                          endpoint:
                            url: https://api.openai.com/v1
                            api_key: sk-...
      responses:
        '200':
          description: >-
            Chat completion in OpenAI format, live or cached. See
            `x-slng-response-source`.
          headers:
            x-slng-request-id:
              $ref: '#/components/headers/XSlngRequestIdResponse'
            x-slng-response-source:
              $ref: '#/components/headers/XSlngResponseSource'
            x-slng-model:
              $ref: '#/components/headers/XSlngModel'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ChatCompletionResponse'
            text/event-stream:
              schema:
                type: string
                description: >
                  OpenAI SSE stream when `stream: true`, ending with `data:
                  [DONE]`. Set

                  `stream_options.include_usage` for a final `usage` chunk.
        '400':
          description: >
            Bad request: missing or malformed agent or session ID
            (`missing_client_id`,

            `invalid_client_id`), unknown pinned `model`, or invalid
            `slng_config`.
          headers:
            x-slng-request-id:
              $ref: '#/components/headers/XSlngRequestIdResponse'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                missingClientId:
                  value:
                    error:
                      message: Missing required agent id
                      type: invalid_request_error
                      code: missing_client_id
        '422':
          description: |
            Unprocessable request: a template variable had no value
            (`missing_template_variables`), `slng_config` was not a JSON object
            (`invalid_slng_config`), or a template variable exceeded its limits.
          headers:
            x-slng-request-id:
              $ref: '#/components/headers/XSlngRequestIdResponse'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                missingTemplateVariables:
                  value:
                    error:
                      message: >-
                        Missing required template variables: appointment_time,
                        city
                      type: invalid_request_error
                      param: template_variables
                      code: missing_template_variables
        '429':
          $ref: '#/components/responses/UpstreamError'
        '502':
          $ref: '#/components/responses/UpstreamError'
        '504':
          $ref: '#/components/responses/UpstreamError'
components:
  parameters:
    XSlngAgentId:
      name: X-Slng-Agent-Id
      in: header
      required: false
      description: >
        Stable agent ID. Scopes the cache — change it when the system prompt
        changes. Required

        unless sent as `slng_agent_id` (body wins). Max 256 chars; no
        whitespace, commas, pipes

        or braces.
      schema:
        $ref: '#/components/schemas/SlngId'
      example: clinic-scheduler-v1
    XSlngSessionId:
      name: X-Slng-Session-Id
      in: header
      required: false
      description: |
        ID for a single call, constant across its turns. Required unless sent as
        `slng_session_id` (body wins). Same character rules as the agent ID.
      schema:
        $ref: '#/components/schemas/SlngId'
      example: call-8f2c1a4e-9b07-4d3a-a1e2-7c5f0d6b2a11
    XSlngRequestId:
      name: X-Slng-Request-Id
      in: header
      required: false
      description: >
        Optional correlation ID, echoed back in `x-slng-request-id`.
        `X-Request-Id` is also

        accepted.
      schema:
        type: string
  schemas:
    ChatCompletionRequest:
      type: object
      required:
        - messages
      additionalProperties: true
      description: >
        OpenAI Chat Completions body plus SLNG extensions. Unlisted OpenAI
        fields pass through.
      properties:
        model:
          type: string
          default: slng/auto
          description: >
            `slng/auto` (or omit) routes across your configured models with
            failover. Pass an

            entry's `model` label to pin one — disabling failover and giving it
            a separate

            cache. Any other value returns a 400.
          examples:
            - slng/auto
            - gpt-4o-mini
        messages:
          type: array
          minItems: 1
          description: >
            OpenAI messages array. `template_variables` substitution touches the
            system message only.
          items:
            $ref: '#/components/schemas/ChatMessage'
        temperature:
          type: number
          description: OpenAI parameter, passed through.
        top_p:
          type: number
          description: OpenAI parameter, passed through.
        max_tokens:
          type: integer
          description: OpenAI parameter, passed through.
        max_completion_tokens:
          type: integer
          description: OpenAI parameter, passed through.
        stream:
          type: boolean
          default: false
          description: >
            `true` for SSE streaming, `false` for a single JSON response.
            Supported for every model.
        stream_options:
          type: object
          additionalProperties: true
          description: Streaming options.
          properties:
            include_usage:
              type: boolean
              default: false
              description: |
                Set `true` while streaming to get the final usage chunk.
        tools:
          type: array
          description: >
            OpenAI tool definitions. Answers with tool calls, and the turn after
            a tool result, are not cached.
          items:
            type: object
            additionalProperties: true
        tool_choice:
          description: OpenAI tool choice, passed through.
          oneOf:
            - type: string
            - type: object
              additionalProperties: true
        template_variables:
          $ref: '#/components/schemas/TemplateVariables'
        slng_config:
          $ref: '#/components/schemas/SlngConfig'
        slng_agent_id:
          allOf:
            - $ref: '#/components/schemas/SlngId'
          description: >
            Body form of `X-Slng-Agent-Id`. Required unless the header is sent;
            wins if both.
        slng_session_id:
          allOf:
            - $ref: '#/components/schemas/SlngId'
          description: >
            Body form of `X-Slng-Session-Id`. Required unless the header is
            sent; wins if both.
        slng_analytics:
          type: boolean
          default: false
          description: >
            Set `true` to record the request and response (in-region) for
            analytics. Requires

            both IDs.
        slng_pure_proxy:
          type: boolean
          default: false
          description: >
            Set `true` to return the provider response with no caching or edits.
            Routing and

            failover still apply.
    ChatCompletionResponse:
      type: object
      additionalProperties: true
      description: >
        OpenAI Chat Completions response, live or cached. `usage` reports the
        cached answer's tokens on a hit, billed tokens on a live answer.
      properties:
        id:
          type: string
        object:
          type: string
          example: chat.completion
        created:
          type: integer
        model:
          type: string
        choices:
          type: array
          items:
            type: object
            additionalProperties: true
            properties:
              index:
                type: integer
              message:
                $ref: '#/components/schemas/ChatMessage'
              finish_reason:
                type: string
        usage:
          type: object
          additionalProperties: true
          properties:
            prompt_tokens:
              type: integer
            completion_tokens:
              type: integer
            total_tokens:
              type: integer
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          properties:
            message:
              type: string
            type:
              type: string
              example: invalid_request_error
            param:
              type:
                - string
                - 'null'
            code:
              type: string
              enum:
                - missing_client_id
                - invalid_client_id
                - missing_template_variables
                - invalid_slng_config
              description: >
                Error code. `slng_config` failures other than a non-object body
                return a 400 with a message beginning `invalid slng_config:`.
    SlngId:
      type: string
      maxLength: 256
      pattern: ^[^\s,|{}]+$
      description: |
        Opaque ID. Max 256 characters; no whitespace, commas, pipes or braces.
    ChatMessage:
      type: object
      required:
        - role
      additionalProperties: true
      properties:
        role:
          type: string
          enum:
            - system
            - user
            - assistant
            - tool
        content:
          description: Message content. String, or the standard OpenAI content-part array.
          oneOf:
            - type: string
            - type: array
              items:
                type: object
                additionalProperties: true
            - type: 'null'
        name:
          type: string
        tool_calls:
          type: array
          items:
            type: object
            additionalProperties: true
        tool_call_id:
          type: string
    TemplateVariables:
      type: object
      maxProperties: 64
      description: >
        SLNG extension. `{name: value}` map substituted into `{{name}}`
        placeholders in the

        **system message only**. Names are letters, digits and underscore.


        Limits (over any returns 422): 64 variables, 64-char names, 4000-char
        values. A

        `{{name}}` with no value returns 422 (`missing_template_variables`);
        unused variables

        are ignored.


        Use for personalization that is spoken or echoed. Values that steer the
        answer

        (language, plan, region) belong in the prompt — steering here can make
        the cache serve

        another caller's answer.
      propertyNames:
        pattern: ^[A-Za-z0-9_]{1,64}$
      additionalProperties:
        type: string
        maxLength: 4000
      example:
        caller_name: Rajesh
        city: Mumbai
    SlngConfig:
      type: object
      required:
        - tiers
      additionalProperties: false
      description: >
        SLNG extension. Model endpoints and provider keys sent inline, replacing
        your saved

        configuration for that request. Treat as a credential (HTTPS, keep out
        of logs); under

        256 KB serialized. Invalid config returns 400 (`invalid slng_config:`);
        a non-object

        returns 422 (`invalid_slng_config`).
      properties:
        tiers:
          type: object
          description: >
            Up to three tiers (`"1"`, `"2"`, `"3"`) tried in order. Within a
            tier, traffic

            splits by `weight` (summing to 100). A failed model (5xx, timeout,
            429) retries

            once against the next option.
          propertyNames:
            enum:
              - '1'
              - '2'
              - '3'
          additionalProperties:
            type: array
            minItems: 1
            items:
              $ref: '#/components/schemas/SlngConfigEntry'
    SlngConfigEntry:
      type: object
      required:
        - model
        - weight
        - endpoint
      additionalProperties: false
      properties:
        model:
          type: string
          description: >
            Label for this entry. Pass it as the request `model` to target it
            directly.
        weight:
          type: integer
          minimum: 1
          maximum: 100
          description: Share of the tier's traffic; weights in a tier sum to 100.
        endpoint:
          $ref: '#/components/schemas/SlngEndpoint'
    SlngEndpoint:
      type: object
      additionalProperties: false
      description: >
        Where the model is served, with its credentials. Required fields depend
        on `provider`;

        fields for another provider are rejected.
      properties:
        provider:
          type: string
          default: openai-compat
          enum:
            - openai-compat
            - openai-responses
            - azure
            - vertex
            - bedrock
          description: >
            `openai-compat` (default) for any OpenAI Chat Completions service
            (OpenAI, Groq,

            vLLM, and so on). `openai-responses` calls the upstream Responses
            API instead — for

            models that only work that way, typically reasoning models that
            refuse tools.
        url:
          type: string
          format: uri
          description: >
            Required for `openai-compat`, `openai-responses` and `azure`. For
            `azure`, the

            resource **root**
            (`https://my-resource.cognitiveservices.azure.com/`), not the

            deployment URL.
        api_key:
          type: string
          description: >
            Required for `openai-compat`, `openai-responses` and `azure`. Your
            provider key, not your SLNG key.
        auth_header:
          type: string
          description: >
            Optional (`openai-compat`). Custom key header, for example
            `x-goog-api-key` for Gemini.
        model_id:
          type: string
          description: >
            Model name sent to the provider when it differs from the `model`
            label. Required for `bedrock`.
        azure_deployment:
          type: string
          description: 'Required for `provider: azure`. The Azure deployment name.'
        api_version:
          type: string
          description: 'Required for `provider: azure`, for example `2024-12-01-preview`.'
        vertex_credentials:
          type: object
          additionalProperties: true
          description: >
            Required for `provider: vertex`. GCP service-account key as a JSON
            object.
        vertex_location:
          type: string
          description: 'Required for `provider: vertex`. The Vertex AI location.'
        vertex_project:
          type: string
          description: >
            Optional for `provider: vertex`. Defaults to the service-account
            key's project.
        aws_access_key_id:
          type: string
          description: 'Required for `provider: bedrock`.'
        aws_secret_access_key:
          type: string
          description: 'Required for `provider: bedrock`.'
        aws_region:
          type: string
          description: 'Required for `provider: bedrock`.'
        aws_session_token:
          type: string
          description: 'Optional for `provider: bedrock`, for temporary credentials.'
      examples:
        - url: https://api.openai.com/v1
          api_key: sk-...
        - provider: azure
          url: https://my-resource.cognitiveservices.azure.com/
          api_key: ...
          azure_deployment: gpt-4o-deploy
          api_version: 2024-12-01-preview
        - provider: bedrock
          aws_access_key_id: AKIA...
          aws_secret_access_key: ...
          aws_region: ap-south-1
          model_id: anthropic.claude-3-5-sonnet-20241022-v2:0
  headers:
    XSlngRequestIdResponse:
      description: >
        The router's ID for this request, on every response. Quote it in support
        requests.
      schema:
        type: string
    XSlngResponseSource:
      description: >
        Answer source: `llm` (live model) or `cache`. `router` and `analytics`
        appear only

        with those features.
      schema:
        type: string
        enum:
          - llm
          - cache
          - router
          - analytics
    XSlngModel:
      description: >
        The model that produced the answer, after any failover. Live answers
        only.
      schema:
        type: string
  responses:
    UpstreamError:
      description: >
        Upstream failure (provider `429`, `502` or `504`). Only
        `x-slng-request-id` is

        returned.
      headers:
        x-slng-request-id:
          $ref: '#/components/headers/XSlngRequestIdResponse'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  securitySchemes:
    SlngApiKey:
      type: http
      scheme: bearer
      description: >
        Send your SLNG API key as a Bearer token (an OpenAI SDK's `api_key`
        option). Provider

        credentials in `slng_config` authenticate to those providers.

````