How to get conversion context/messages in function...
# ask-ai
p
How to get conversion context/messages in function tool of an AgentTask
t
Thanks for reaching out, Harshad. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you retrieve conversation context or messages within the function tool of an
AgentTask
, 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:
Copy code
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 Docs
p
how does the items are sorted, do I need to sort it by the created_at time to compose the conversion history between user and assistant?
t
I'm glad you asked about the
chat_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.snap
Just to share a helpful tip: the
chat_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!
p
emm, but I am getting error: chat_ctx: ChatContext = self.session.chat_ctx ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'AgentSession' object has no attribute 'chat_ctx' {"room": "voice_assistant_room_3892", "function": "notify_survey_completed", "speech_id": "speech_18309a8cd9f2", "pid": 7921, "job_id": "AJ_EnL9e3Vuj4Ff"}
t
It looks like the error is happening because the
AgentSession
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:
Copy code
# 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