Agent callbacks enable your application automatically trigger preset actions or responses when specific events occur. This topic explains how to use them.
Overview
When an AI agent is running and a specific event occurs, Alibaba Cloud automatically sends a request to your server, enabling you to process the event using your custom business logic.
Configure agent callbacks
Go to the AI Agents page in the console, find the agent to configure, and click Manage in the Actions column.
On the Callback Configurations tab, click Enable to configure agent callbacks. Select the event types and set the Callback URL and an optional Authentication Token.
NoteThe token is placed in the
Authorizationheader for authentication. Your server should verify this token to ensure the request is from a trusted source.
Click OK to save the configuration.
Callback payload fields
Name | Type | Required | Description | Example |
aiAgentId | String | Yes | The agent ID. | xxxxx |
instanceId | String | Yes | The unique ID of the agent instance. | 39f8e0bc005e4f309379701645f4**** |
event | String | Yes | The event type.
| agent_start |
data | Json | No | The data payload, such as chat records. | |
code | String | Yes | The status code for the callback event. For details, see Callback event status code. | 1001 |
message | String | Yes | The callback message. | User has been kicked from the room |
timestamp | String | Yes | The timestamp for when the callback event occurred. | 2023-10-01T12:00:00Z |
userData | String | No | User-defined information. | |
extendData | Json | No | Custom extended information. |
Callback examples
Workflow status callback
For workflow status events, the extendData fields are as follows:
Name | Type | Description |
channelId | String | The channel ID. |
sentenceId | Int | A unique ID for a conversational turn. Note Multiple agent responses to a single user question share the same sentenceId. |
requestTimestamp | String |
|
responseTimestamp | String |
|
Server example
Python
from aiohttp import web
import json
from loguru import logger
async def handle_post(request):
"""
Handles POST requests and logs the received data.
"""
# Get the Authorization header from the request.
authorization_header = request.headers.get('Authorization')
if authorization_header is None or not authorization_header.startswith('Bearer fixed-token'):
logger.error("Unauthorized request")
return web.Response(status=401, text='Unauthorized')
try:
# Get data from the request body.
callback_data = await request.json()
logger.info("Parsed JSON data:")
logger.info(json.dumps(callback_data, indent=4))
return web.Response(text='Callback received successfully', status=200)
except json.JSONDecodeError:
# If JSON parsing fails, return an error response.
return web.Response(text='Invalid JSON', status=400)
app = web.Application()
app.add_routes([web.post('/', handle_post)])
if __name__ == '__main__':
web.run_app(app, host='localhost', port=8081) Callback event status codes
Status code | Callback event | Callback message |
1001 | Agent starts | AI Agent starts. |
1002 | Agent stops | AI Agent ends. |
1003 | Session starts | Session starts |
4001 | Concurrent agent routes exhausted | Concurrent routes exhausted |
4002 | Agent kicked from the channel | User has been kicked from the room |
4003 | Invalid agent token | Invalid token for the AI agent |
4004 | Agent stream subscription failed | Failed to pull stream for the AI agent |
4005 | Third-party ASR failed | error description |
4006 | Avatar service is unavailable | Avatar service is not available |
8001 | Intent recognized | Intent recognized event |
8002 | LLM data received | LLM data received event |
8003 | TTS data received | TTS data received event |