logo
On this page

AI Short-Term Memory (Agent Context) Management

When creating an agent instance and implementing role-playing conversations, the agent can remember recent chat interactions (commonly known as short-term memory), which is implemented through the context of LLM (Large Language Model). During voice conversations, memory has the following stages:

Warning
  • Context Length Limit: LLMs typically have a maximum length limit, exceeding which will result in an error. For example, "skylark-1.5-pro-32k" allows no more than 32k tokens.

  • Context Length vs. Inference Time: Generally, longer context results in slower LLM output.

  • Context Length vs. Cost: LLM pricing is usually based on context length for input billing - longer length means higher cost.

Implementation Steps

Set Initial Memory

When creating an agent instance, you can set the initial memory (initial context) for the agent instance. Memory sources can be either "In-app chat conversation history" or "custom external context", controlled through the MessageHistory parameter of the create agent instance API. The detailed parameter structure is as follows:

Load Initial Memory from In-app chat conversation History

  1. Ensure you have enabled the In-app chat service.
  2. When creating an agent instance, set SyncMode to 0 and fill in the corresponding In-app chat parameters. Example:
Note
In-app chat.RobotId is the UserInfo.UserId used when calling the In-app chat API to register a bot.
 "MessageHistory": {
// !mark(1:4)
    "SyncMode": 0,
    "In-app chat": {
        "RobotId": "@RBT#123",
        "LoadMessageCount": 10
    }
 }

Load Initial Memory from Custom External Context

  1. Business needs to store context information itself.
  2. When creating an agent instance, set SyncMode to 1 and fill in the Messages parameter. Example:
 "MessageHistory":{
// !mark(1:19)
    "SyncMode": 1,
    "Messages": [
        {
            "Role": "user",
            "Content": "What's your name?"
        },
        {
            "Role": "assistant",
            "Content": "My name is Skylark."
        },
        {
            "Role": "user",
            "Content": "Tell me a story."
        },
        {
            "Role": "assistant",
            "Content": "Sure, let me tell you the story of 'The Three Little Pigs'."
        }
    ]
}

Manage Memory During Voice Call

Managing memory during voice calls means managing the agent instance context. You can get the context or reset it.

Get Agent Instance Context List

Call the GetAgentInstanceMsgList API with the AgentInstanceId returned after creating an agent instance. The server will return the context of the AI agent instance, with messages sorted by chat time in ascending order.

Sample
{
    "Code": 0,
    "Message": "success",
    "RequestId": "2537521374375652066",
    "Data": {
        "Total": 4,
        "MessageList": [
            {
                "Role": "user",
                "Content": "What's your name?"
            },
            {
                "Role": "assistant",
                "Content": "My name is Skylark."
            },
            {
                "Role": "user",
                "Content": "Tell me a story."
            },
            {
                "Role": "assistant",
                "Content": "Sure, let me tell you the story of 'The Three Little Pigs'."
            }
        ]
    }
}

Reset Agent Instance Context List

Call the ResetAgentInstanceMsgList API with the AgentInstanceId returned after creating an agent instance. The server will reset the context of the AI agent instance.

Add Message to Agent Instance Context List

Call the AddAgentInstanceMsg API with the AgentInstanceId returned after creating an agent instance. The server will insert the Messages value after the last message in the current instance's context list.

{
    "AgentInstanceId": "1907755175297171456",
    "Messages": [
        {
            "Role": "user",
            "Content": "Hello, I want to learn about product information"
        },
        {
            "Role": "assistant",
            "Content": "You just introduced a skincare product"
        }
    ]
}

Archive Memory After Voice Call

Simply set SyncMode to 0 and provide a valid In-app chat bot ID as In-app chat.RobotId when creating an agent instance, and the voice call conversation records will be stored in the In-app Chat service.

These stored chat history messages can be used as initial memory for subsequent conversations. Read Load Initial Memory from In-app chat conversation History. You can also maintain memory archives in other ways according to your business needs.

Sample
"MessageHistory": {
// !mark(1:4)
    "SyncMode": 0,
    "In-app chat": {
        "RobotId": "@RBT#123"
    }
}

Previous

Speech Segmentation Control

Next

Voice Interruption Sensitivity Adjustment