Skip to main content
Observability helps you understand how model router handles requests, verify routing behavior, and investigate latency, errors, and fallback. Request-level signals complement aggregate metrics and logs, giving developers and operators context to evaluate application performance. This article covers the per-request routing metadata preview for the Chat Completions API. The metadata identifies the serving model and describes routing attempts for an individual request. For aggregate metrics and logs, see Monitor model deployments.

Prerequisites

  • Python 3.9 or later.
  • The openai>=1.75.0 and python-dotenv packages. Install them by running pip install "openai>=1.75.0" python-dotenv.
  • A model router deployment that you can access through an Azure OpenAI endpoint.
  • The endpoint and API key for your Azure OpenAI resource. The complete sample reads them from the AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_API_KEY environment variables.
  • Azure OpenAI API version 2024-10-21.

Enable per-request routing metadata

After your application reads the endpoint and API key into endpoint and api_key, create the client with the preview feature header:
The Foundry-Features: ModelRouterControls=V1Preview header requests per-request routing metadata. Because this feature is in preview, the metadata presence and response schema can vary by request and service version.

Send a Chat Completions request

Use the model router deployment name to send a request. The response includes the completion and, when available, the per-request routing metadata:

Understand routing metadata

The following model_selection_details fragment illustrates a request with two ordered model attempts:
  • mode is the routing mode returned for the request.
  • routing_trace contains the routing entries returned for the request.
  • latency_ms is the latency reported for a routing-trace entry.
  • attempts lists model attempts in order.
  • Each attempt contains a model and an HTTP status in result.status.
  • A failed attempt can include an optional error with a code and message.

Extract routing and fallback information

After the Chat Completions request returns response, inspect the serving model and model selection details:
Ordered attempts can reveal automatic fallback for an individual request. In the example response, the failed attempt followed by a successful attempt is evidence of fallback for that request. Requests don’t always include multiple attempts, so don’t expect fallback on every request. For complete application setup and runnable examples, see the Foundry Model Router samples.

Interpret session affinity metadata

When you enable the Chat Completions session affinity preview, model_router_details can include a session_affinity object. The following response fragment shows a request that retained its associated model:
Interpret the fields as follows: When decision is switch, inspect routing_trace and the top-level model field together. The following fragment shows an associated model that returned a retryable response before fallback selected another model:
If affinity lookup or persistence isn’t available, inference continues through normal routing and the response omits the complete session_affinity object. Don’t infer an affinity decision when the object or decision field is absent.

Interpret the results

The following output shows the response and routing metadata for an example request:
  • If model_selection_details is absent, the sample reports that no model selection details were returned. Don’t infer routing details that aren’t present.
  • If routing_trace is empty, the sample reports that no routing trace was returned.
  • An attempt can omit error. The extraction code prints an error only when the response includes one.
  • Model names, HTTP statuses, attempt counts, and reported latency can vary by request and service version.
  • response.model identifies the serving model for the demonstrated request.