Skip to main content

Integrate short-term memory

Short-term memory enables your conversational agent to maintain context and coherence by storing structured conversation data during a session. This includes not only message content and roles, but also turn tracking, interruption handling, timestamps, and source metadata.

Built on the OpenAI Chat Completions format with conversational AI extensions, short-term memory integrates seamlessly with large language models while providing flexibility for custom implementations, long-term storage, and dynamic memory updates.

This guide shows you how to access, use, and implement short-term memory in your applications.

info

This guide applies to Conversational AI Engine version 1.4 and above.

Understand the tech

Conversational AI Engine stores short-term memory in JSON format, following the OpenAI Chat Completions structure. The following example shows the data structure:


_63
{
_63
"contents": [
_63
{
_63
"role": "assistant",
_63
"content": "How can I help you today?",
_63
"turn_id": 1,
_63
"timestamp": 1678901234,
_63
"metadata": {
_63
"source": "greeting"
_63
}
_63
},
_63
{
_63
"role": "user",
_63
"content": "Can you tell me a joke?",
_63
"turn_id": 2,
_63
"timestamp": 1678901235,
_63
"metadata": {
_63
"source": "asr",
_63
"user": "user123"
_63
}
_63
},
_63
{
_63
"role": "assistant",
_63
"content": "Why did the scarecrow ",
_63
"turn_id": 2,
_63
"timestamp": 1678901236,
_63
"metadata": {
_63
"interrupted": true,
_63
"interrupt_timestamp": 1678905225,
_63
"original": "Why did the scarecrow win an award? Because he was outstanding in his field!",
_63
"source": "llm"
_63
}
_63
},
_63
{
_63
"role": "user",
_63
"content": "You know what? Tell me a story instead.",
_63
"turn_id": 3,
_63
"timestamp": 1678905235,
_63
"metadata": {
_63
"source": "asr",
_63
"user": "user123"
_63
}
_63
},
_63
{
_63
"role": "assistant",
_63
"content": "Once upon a time in a land far away, there lived a brave knight who fought dragons and saved princesses.",
_63
"turn_id": 3,
_63
"timestamp": 1678905236,
_63
"metadata": {
_63
"source": "llm"
_63
}
_63
},
_63
{
_63
"role": "assistant",
_63
"content": "Are you still there?",
_63
"turn_id": 4,
_63
"timestamp": 1678905236,
_63
"metadata": {
_63
"source": "command"
_63
}
_63
}
_63
]
_63
}

Each message contains both standard OpenAI fields and enhanced fields that provide additional context for conversational AI scenarios:

  • OpenAI standard fields:

    • role: Specifies the message sender's role in the conversation. Only user and assistant (agent) are supported in short-term memory. system messages are not included.

    • content: The specific text content. Currently, short-term memory does not consider multimodal input.

  • Conversational AI Engine extensions:

    • turn_id: Dialogue turn identifier. Starts from 0 and increments with each dialogue turn between the user and agent.

    • timestamp: The timestamp of the corresponding message, with millisecond accuracy.

    • metadata: Metadata of the message containing the following fields:

      • source: Indicates how the message was generated:

        ValueDescriptionuser
        message
        assistant
        message
        asrSpeech recognition result
        messageText message
        commandMessages generated by RESTful API call
        llmLarge Language Model
        greetingGreeting message
        llm_failureLLM call failed
        silenceSilent reminder message
      • interrupted: Whether this assistant message was interrupted by human voice:

        • true: This message was interrupted.
        • false (default): This message was not interrupted. The field is hidden when false.
      • interrupt_timestamp: The timestamp when the agent message was interrupted, with millisecond precision. Only exists when interrupted is true.

      • original: The complete content actually generated by the LLM. Only exists when interrupted is true.

Access short-term memory

Conversational AI Engine provides the following methods to access short-term memory:

  • During agent runtime: Call the Retrieve agent history API to retrieve the agent's complete short-term memory in JSON format. This API returns all short-term memory stored during the agent's lifecycle.

  • After agent stops: Agora sends short-term memory to your business server through the message notification service. For details, see Notification event types.

Pass memory content to the LLM

Depending on the llm.vendor field you specify when creating the agent, Conversational AI Engine uses different strategies to pass memory content to the LLM.

Pass memory content to the LLM

Non-custom scenario

When llm.vendor is not "custom", the engine only transfers the OpenAI standard fields (role and content) from short-term memory to ensure compatibility.


_28
{
_28
"messages": [
_28
{
_28
"role": "assistant",
_28
"content": "How can I help you today?"
_28
},
_28
{
_28
"role": "user",
_28
"content": "Can you tell me a joke?"
_28
},
_28
{
_28
"role": "assistant",
_28
"content": "Why did the scarecrow "
_28
},
_28
{
_28
"role": "user",
_28
"content": "You know what? Tell me a story instead."
_28
},
_28
{
_28
"role": "assistant",
_28
"content": "Once upon a time in a land far away, there lived a brave knight who fought dragons and saved princesses."
_28
},
_28
{
_28
"role": "assistant",
_28
"content": "Are you still there?"
_28
}
_28
]
_28
}

Custom scenario

When llm.vendor is set to "custom", the engine transmits all fields in short-term memory to the LLM. You can implement a wrapper to filter or merge extended fields as needed. For implementation details, see Custom LLM.

Example use cases:

  • Add timestamps: Include message timestamps in the content field
  • Add user context: Prepend user information to the content field
  • Handle interruptions: Provide the complete original content for interrupted messages

Combine these enhancements with system_messages to give your LLM deeper conversational awareness, allowing it to maintain user context and gracefully handle interrupted responses.


_67
{
_67
"messages": [
_67
{
_67
"role": "assistant",
_67
"content": "How can I help you today?",
_67
"turn_id": 1,
_67
"timestamp": 1678901234,
_67
"metadata": {
_67
"source": "greeting"
_67
}
_67
},
_67
{
_67
"role": "user",
_67
"content": "Can you tell me a joke?",
_67
"turn_id": 2,
_67
"timestamp": 1678901235,
_67
"metadata": {
_67
"source": "asr",
_67
"user": "user123"
_67
}
_67
},
_67
{
_67
"role": "assistant",
_67
"content": "Why did the scarecrow ",
_67
"turn_id": 2,
_67
"timestamp": 1678901236,
_67
"metadata": {
_67
"interrupted": true,
_67
"interrupt_timestamp": 1678905225,
_67
"original": "Why did the scarecrow win an award? Because he was outstanding in his field!",
_67
"source": "llm"
_67
}
_67
},
_67
{
_67
"role": "user",
_67
"content": "You know what? Tell me a story instead.",
_67
"turn_id": 3,
_67
"timestamp": 1678905235,
_67
"metadata": {
_67
"source": "asr",
_67
"user": "user123"
_67
}
_67
},
_67
{
_67
"role": "assistant",
_67
"content": "Once upon a time in a land far away, there lived a brave knight who fought dragons and saved princesses.",
_67
"turn_id": 3,
_67
"timestamp": 1678905236,
_67
"metadata": {
_67
"source": "llm"
_67
}
_67
},
_67
{
_67
"role": "assistant",
_67
"content": "Are you still there?",
_67
"turn_id": 4,
_67
"timestamp": 1678905236,
_67
"metadata": {
_67
"source": "command"
_67
}
_67
}
_67
],
_67
"turn_id": 4,
_67
"timestamp": 1678905236,
_67
"interruptable": true,
_67
"model": "xxxx"
_67
}

Convert to long-term memory

Short-term memory disappears when the agent stops. To preserve this data:

  1. Store short-term memory: Save the short-term memory to your server after the agent stops.
  2. Inject into new sessions: When creating a new agent, use llm.system_messages to inject either the original memory content or a summarized version.

The following example shows how to inject summarized memory content using the system_messages array:


_10
[
_10
{
_10
"role": "system",
_10
"content": "You are a helpful assistant. xxx"
_10
},
_10
{
_10
"role": "system",
_10
"content": "Previously, user has talked about their favorite hobbies with some key topics: xxx"
_10
}
_10
]

Update memory at runtime

Starting with version 1.4, you can call the Update agent configuration API to update the agent's llm.system_messages field while the agent is running. This enables you to update the memory content dynamically.