All Products
Search
Document Center

API Gateway:Configure an AI log policy

Last Updated:Apr 15, 2026

The AI log policy lets you select log fields on demand, add fields in batches using built-in templates, and define custom log fields with JSON PATH configurations. This helps you control logging scope and reduce storage costs. This feature applies to both compatible protocols, such as OpenAI and Model Studio, and custom HTTP protocols, though the configuration capabilities differ between them.

Prerequisites

Protocol types and configuration capabilities

AI Gateway offers different log policy configurations depending on the protocol type of the Model API or Agent API:

Capability

Compatible protocols

Custom HTTP protocol

Predefined basic fields

Supported

Not supported

Batch add from template

Supported

Supported

Custom JSON PATH fields

Supported

Supported

Enable or disable individual fields

Supported

Supported

The JSON PATH for a specific field can vary across paths due to different API request or response structures.

Note

For compatible protocols, the following protocols and scenarios support predefined basic fields:

  • Model API: Text generation and image generation scenarios for OpenAI and Model Studio.

  • Agent API: Model Studio applications.

Enable the AI request log

  1. Log on to the API Gateway console.

  2. In the left-side navigation pane, click AI Gateway > Instance.

  3. On the AI Gateway page, click the name of the target instance to open its details page.

  4. Click the Model API tab, and then click the name of the target Model API in the list.

  5. On the API details page, click the Policies and Plug-ins tab.

  6. In the Preset Policies and Plug-ins section, find the AI Request Log panel, turn on the switch, and then click the expand icon.

  7. Configure the log fields based on the protocol type of the Model API:

  8. After you complete the configuration, click Save.

Configure log fields for compatible protocols

For a Model API that uses a compatible protocol, such as OpenAI, the AI request log panel lets you manage basic fields, add fields in batches from a template, and create custom fields.

Panel layout

Area

Description

Enable switch

Enables or disables the AI request log feature.

Path list

Lists all paths for the API. Selecting a different path updates the field configuration on the right.

Basic fields

Basic fields primarily cover four categories: Conversation, Metadata, Usage, and Tools.

Custom fields

Custom log fields that you add manually.

Action buttons

  • Batch Add from Template: Imports fields in batches from a built-in template.

  • Add Custom Field: Adds a field manually.

Note
  • The Path list contains all paths that the current API supports. Typical OpenAI-compatible paths include /v1/chat/completions and /v1/completions. When a backend service model has multiple services, a single path may correspond to multiple routes. Multiple routes for the same path share the same configuration.

  • You can enable or disable each field using the switch in its row. The table header shows the number of enabled fields and the total number of fields, for example, "Basic fields (14/24)". Click Show Only Enabled Fields to filter the list and display only enabled fields.

Basic fields

The main basic fields are organized by category as follows:

Category

Field example

Description

Conversation

question, answer

The user's question and the model's response content.

Metadata

request_model, model, chat_id

The requested model name, the returned model name, and the session ID.

Usage

input_token, output_token, total_token, input_token_details, output_token_details, input_tokens_details.cached_tokens, output_tokens_details.reasoning_tokens

Token usage data for the request and response.

Tools

tool_use

Tool invocation information.

Note
  • Field logging location: Basic fields are written to the log with a fixed key (the field name). Except for answer and question, all other basic fields are stored as top-level fields within the ai_log field. The answer and question fields are extracted from ai_log and stored as separate, standalone fields.

  • System built-in fields: The model, chat_id, input_token, input_token_details, output_token, output_token_details, and total_token fields are system built-in fields. These fields are enabled by default. During field extraction, the system first attempts to extract them by using the configured JSON PATH. If no JSON PATH is configured, the system uses built-in rules to extract them.

  • Streaming and non-streaming fields: Most response fields can be extracted from both non-streaming and streaming responses, each requiring a different JSON PATH configuration. For example, for the answer field under the /v1/chat/completions path, the extraction path for a non-streaming response is choices.0.message.content, while the path for a streaming response is choices.0.delta.content.

JSON PATH configuration

JSON PATH configuration syntax

The JSON PATH configuration uses GJSON syntax to extract specified field values from the JSON data of an API request or response.

Syntax description:

  • Use a period (.) to access a field in an object.

  • Access array elements by index, with 0 being the first index.

  • Multi-level nested access is supported.

For example, choices.0.message.content means:

  • First, obtain the choices array.

  • Then, retrieve the element at index 0.

  • Then, sequentially retrieve the value of the message.content field.

Example:

The following is a sample response:

{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "I am a large-scale language model developed by Alibaba Cloud. My name is Qwen."
      },
      "finish_reason": "stop",
      "index": 0
    }
  ],
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 3019,
    "completion_tokens": 104,
    "total_tokens": 3123,
    "prompt_tokens_details": {
      "cached_tokens": 2048
    }
  },
  "model": "qwen-plus",
  "id": "chatcmpl-6ada9ed2-7f33-9de2-8bb0-78bd4035025a"
}

The following table shows sample JSON PATH configurations for the corresponding fields:

Field name

JSON PATH

Extracted result

answer

choices.0.message.content

I am an ultra-large-scale language model developed by Alibaba Cloud. My name is Qianwen.

model

model

qwen-plus

input_token

usage.prompt_tokens

3019

JSON PATH differences across paths and sources

A Model API may be associated with multiple paths, such as /v1/chat/completions, /v1/completions, and /v1/responses. The JSON PATH for the same field can differ depending on the path and source. The following table shows examples for three representative fields: question, answer, and input_token.

Field

Source

/v1/chat/completions

/v1/completions

/v1/responses

question

Request

messages.@reverse.#(role=="user").content

prompt

input

answer

non-streaming response

choices.0.message.content

choices.0.text

output.#(type=="message").content.#(type=="output_text").text

answer

streaming response

choices.0.delta.content

choices.0.text

delta

input_token

non-streaming response

usage.prompt_tokens

usage.prompt_tokens

usage.input_tokens

input_token

streaming response

usage.prompt_tokens

usage.prompt_tokens

response.usage.input_tokens

Note
  • When you switch between paths in the Path list, the JSON PATH for the basic fields updates automatically. Modifying the field configuration for one path does not affect other paths.

  • To modify the JSON PATH for a field, click Edit in the Actions column for that field. In the dialog box that appears, modify the JSON PATH value. The system extracts the field from the API request or response based on the configured JSON PATH.

Batch add fields from a template

The AI request log feature includes built-in templates to add predefined fields in batches.

  1. In the AI request log panel, click Batch Add from Template.

  2. Select Select Template, select the fields to add, and click Batch Add.

There are 15 built-in templates, divided into the following two groups by provider:

OpenAI templates (6)

Use case

Path

Text generation

/v1/chat/completions

/v1/completions

/v1/responses

Image generation

/v1/images/generations

/v1/images/edits

/v1/images/variations

Model Studio templates (9)

Use case

Path

Text generation

/api/v1/services/aigc/text-generation/generation

Image generation

/api/v1/services/aigc/text2image/image-synthesis

/api/v1/services/aigc/image2image/image-synthesis

/api/v1/services/aigc/image2image/out-painting

/api/v1/services/aigc/virtualmodel/generation

/api/v1/services/aigc/background-generation/generation

/api/v1/services/aigc/multimodal-generation/generation

/api/v1/tasks

Agent application

/api/v1/apps/{app_id}/completion

Note
  • Built-in templates offer significantly more optional fields than basic fields. For example, the OpenAI /v1/chat/completions template provides more than 200 optional fields. These fields are grouped into categories, such as conversation, configuration, tools, usage, metadata, candidates, identifiers, cache, multimodality, and probability, which helps you select and configure fields quickly.

  • For actual field definitions and their meanings, refer to the API documentation for the corresponding protocol.

Add a custom field

In addition to basic fields and template fields, you can also add custom fields manually.

  1. In the AI request log panel, click Add Custom Field.

  2. Configure the following parameters:

    Parameter

    Description

    Field name

    The custom field name is used as the Key within the ai_log field of the log.

    Source

    The source of the field. Valid values: Request, non-streaming response, and streaming response.

    JSON PATH

    The JSON PATH used to extract the field value from the source data.

    Description

    A description of the field's purpose.

  3. Click Confirm to complete the configuration.

After you add a custom field, it appears on the Custom Fields tab. Use Edit and Delete in the Actions column to manage existing fields.

Configure logs for a custom HTTP protocol

For a Model API that uses a custom HTTP protocol, the AI request log does not provide basic fields with predefined JSON PATH configurations. You must define the fields you want to log according to the request and response structure of your protocol. You can add fields manually or import fields from a template.

Use the following steps:

  1. In the AI request log panel, click Add Custom Field and define the fields to log according to the request and response structure of your protocol.

  2. Specify the correct JSON PATH for each field to ensure that the path matches the actual data structure.

  3. You can also import a template for a similar protocol as a starting point, and then adjust the JSON PATHs to match your protocol's structure.

Configuration examples

Example 1: Log core fields for OpenAI-compatible protocols

This example shows how to log non-streaming conversation content and token usage for the /v1/chat/completions path to reduce log size.

  1. Go to the Policies and Plug-ins tab of the target Model API and expand the AI Request Log panel.

  2. Select the /v1/chat/completions path.

  3. On the Basic Fields tab, enable the following fields:

    Field name

    Source

    Category

    Purpose

    Enabled by default

    question

    Request

    Conversation

    Logs the user's question.

    No

    answer

    non-streaming response

    Conversation

    Logs the model's answer in a non-streaming response.

    No

    input_token

    non-streaming response

    Usage

    Logs the number of input tokens.

    Yes

    output_token

    non-streaming response

    Usage

    Logs the number of output tokens.

    Yes

    total_token

    non-streaming response

    Usage

    Logs the total number of tokens.

    Yes

  4. Click Save.

Example 2: Batch add Model Studio fields from template

This example shows how to quickly add log fields for the Model Studio text generation protocol using a built-in template.

  1. Go to the Policies and Plug-ins tab of the target Model API and expand the AI Request Log panel.

  2. Click Batch Add from Template.

  3. Set Select Template to /api/v1/services/aigc/text-generation/generation.

  4. Select the checkboxes for the fields that you need, and then click Batch Add.

  5. Based on your requirements, enable or disable specific fields in the field list, or modify their JSON PATHs.

  6. Click Save.