how do i access chat_ctx inside a tool in my agent
# ask-ai
c
how do i access chat_ctx inside a tool in my agent
✅ 1
t
Thanks for reaching out, Kushagra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you're asking how to access
chat_ctx
inside a tool. You can get it from the
RunContext
passed into the tool function:
context.session.chat_ctx
. Example:
Copy code
@function_tool()
async def my_tool(self, context: RunContext):
    # read current chat context
    current_context = context.session.chat_ctx

    # modify or copy before passing on
    new_ctx = current_context.copy()
    # ...use new_ctx or current_context as needed
Notes: • Each agent has its own `chat_ctx`; the full session history is in
session.history
. • Use
chat_ctx.copy()
when passing context to tasks or handoffs to avoid mutating the original. Sources: Agents and handoffs | LiveKit Documentation | Function tool definition | LiveKit Documentation | Chat context | LiveKit Documentation | Tasks and task groups | LiveKit Documentation, with interactive elements
c
can i not use self.chat_ctx here
t
Good question — regarding whether you can use
self.chat_ctx
inside a tool: you should not rely on
self.chat_ctx
there. Inside a tool the correct place to get the chat context is the
RunContext
passed into the tool:
context.session.chat_ctx
. Quick reasons: • Tools receive
context
(the
RunContext
) rather than the agent instance, so
self.chat_ctx
may not be set or reflect the current session. • Use
context.session.chat_ctx
and
chat_ctx.copy()
if you need to modify or pass a snapshot to tasks/handoffs. If you want, tell me the tool signature you're using and I can show the exact snippet for it. , with interactive elements
c
how do i get the full session chat context
t
You're asking how to get the full session chat context (the complete conversation history). Use the session's
history
rather than an agent's
chat_ctx
. Examples: Python (inside a tool with
context: RunContext
):
Copy code
full_history = context.session.history
Node.js (inside a tool):
Copy code
const fullHistory = context.session.history;
Notes: •
chat_ctx
is per-agent turn;
session.history
contains the full conversation across agents. • If you need a mutable snapshot, copy it before editing. Sources: Chat context | LiveKit Documentation, with interactive elements