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
You have activated Alibaba Cloud API Gateway.
You have created an AI Gateway instance. For more information, see Create a gateway instance.
You have configured at least one Model API or Agent API. For more information, see Manage Model API and Manage Agent API.
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.
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
Log on to the API Gateway console.
In the left-side navigation pane, click .
On the AI Gateway page, click the name of the target instance to open its details page.
Click the Model API tab, and then click the name of the target Model API in the list.
On the API details page, click the Policies and Plug-ins tab.
In the Preset Policies and Plug-ins section, find the AI Request Log panel, turn on the switch, and then click the expand icon.
Configure the log fields based on the protocol type of the Model API:
Compatible protocols (such as OpenAI): See Configure log fields for compatible protocols.
Custom HTTP protocol: See Configure log fields for a custom HTTP protocol.
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 |
|
The Path list contains all paths that the current API supports. Typical OpenAI-compatible paths include
/v1/chat/completionsand/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. |
Field logging location: Basic fields are written to the log with a fixed key (the field name). Except for
answerandquestion, all other basic fields are stored as top-level fields within theai_logfield. Theanswerandquestionfields are extracted fromai_logand stored as separate, standalone fields.System built-in fields: The
model,chat_id,input_token,input_token_details,output_token,output_token_details, andtotal_tokenfields 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
answerfield under the/v1/chat/completionspath, the extraction path for a non-streaming response ischoices.0.message.content, while the path for a streaming response ischoices.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
0being the first index.Multi-level nested access is supported.
For example, choices.0.message.content means:
First, obtain the
choicesarray.Then, retrieve the element at index
0.Then, sequentially retrieve the value of the
message.contentfield.
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 |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
question | Request |
|
|
|
answer | non-streaming response |
|
|
|
answer | streaming response |
|
|
|
input_token | non-streaming response |
|
|
|
input_token | streaming response |
|
|
|
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.
In the AI request log panel, click Batch Add from Template.
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 |
|
| |
| |
Image generation |
|
| |
|
Model Studio templates (9)
Use case | Path |
Text generation |
|
Image generation |
|
| |
| |
| |
| |
| |
| |
Agent application |
|
Built-in templates offer significantly more optional fields than basic fields. For example, the OpenAI
/v1/chat/completionstemplate 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.
In the AI request log panel, click Add Custom Field.
Configure the following parameters:
Parameter
Description
Field name
The custom field name is used as the Key within the
ai_logfield 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.
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:
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.
Specify the correct JSON PATH for each field to ensure that the path matches the actual data structure.
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.
Go to the Policies and Plug-ins tab of the target Model API and expand the AI Request Log panel.
Select the
/v1/chat/completionspath.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
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.
Go to the Policies and Plug-ins tab of the target Model API and expand the AI Request Log panel.
Click Batch Add from Template.
Set Select Template to
/api/v1/services/aigc/text-generation/generation.Select the checkboxes for the fields that you need, and then click Batch Add.
Based on your requirements, enable or disable specific fields in the field list, or modify their JSON PATHs.
Click Save.