polite-scientist-31915
09/11/2025, 2:38 PMtall-airline-16756
09/11/2025, 2:38 PMtall-airline-16756
09/11/2025, 2:40 PMAgentTask
, you can easily access the session's chat_ctx
property. This holds the full conversation history as a list of ChatItem
objects, which includes messages, function calls, and outputs.
Here’s a simple example to guide you:
from livekit.agents import Agent, function_tool
from livekit.agents.llm import ChatContext
class MyAgent(Agent):
@function_tool
async def analyze_conversation(self) -> str:
# Access the full conversation history
chat_ctx: ChatContext = self.session.chat_ctx
messages = [item for item in chat_ctx.items if item.type == "message"]
# Example: Return the last user message
last_user_message = next(
(msg for msg in reversed(messages) if msg.role == "user"),
None
)
if last_user_message:
return f"Last user message was: {last_user_message.text_content}"
return "No prior user message found."
To break it down:
• self.session.chat_ctx.items
gives you all the conversation items.
• You can filter these by type
(like "message") and role
(such as "user").
• The text_content
property contains the actual text of a message.
If anything is unclear or you’d like me to walk you through it step-by-step, please don’t hesitate to ask. I’m here to help!
Sources: Workflows | LiveKit Docs | livekit-agents/livekit/agents/llm/tool_context.py | External data and RAG | LiveKit Docspolite-scientist-31915
09/11/2025, 2:49 PMtall-airline-16756
09/11/2025, 2:49 PMchat_ctx.items
in LiveKit! They are thoughtfully sorted automatically by their created_at
timestamp, which means your conversation history is already arranged in chronological order without you having to do extra sorting.
Whenever new items come in, LiveKit gently places them in the right spot using methods like find_insertion_index
, based on their created_at
value. This way, the natural flow of the conversation between you and the assistant stays just as it should.
If you have any more questions or want me to walk you through anything else, please feel free to ask—I’m here to help!
Sources: livekit-agents/livekit/agents/llm/_provider_format/utils.py | agents/src/llm/__snapshots__/chat_context.test.ts.snaptall-airline-16756
09/11/2025, 2:49 PMchat_ctx.items
in LiveKit are automatically sorted by their created_at
timestamp. This means your conversation history is already organized in chronological order, so you don’t have to worry about sorting it yourself.
When new items come in, LiveKit smartly uses methods like find_insertion_index
to place them exactly where they belong based on their created_at
value. This way, the conversation between the user and assistant stays smooth and in the right sequence.
If you have any more questions or want me to explain anything further, please don’t hesitate to ask—I’m here to help!polite-scientist-31915
09/13/2025, 3:55 AMtall-airline-16756
09/13/2025, 3:55 AMAgentSession
object doesn’t have a direct chat_ctx
attribute. Instead, you can access the conversation history through the history
property of the session.
To fix this, simply replace self.session.chat_ctx
with self.session.history
. Here’s a quick example:
# Correct way to access chat context
chat_context = self.session.history
If you want to update or modify the chat context, you can use methods like update_chat_ctx
on the agent, or manage it dynamically during the conversation with lifecycle hooks such as on_user_turn_completed
.
Please feel free to reach out if you’d like me to explain anything further or help you implement this—I’m here to help!
Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | External data and RAG | LiveKit Docs | External data and RAG | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs