how do i access the current agent inside a tool us...
# ask-ai
c
how do i access the current agent inside a tool using context.
✅ 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 — I’ll help with how to access the current agent inside a tool using
context
. You can get the current agent from the tool’s
context.session.current_agent
. Example in Python:
Copy code
@function_tool()
async def my_tool(self, context: RunContext):
    current_agent = context.session.current_agent
    # use current_agent as needed
If you want an example for another language or need details on properties available on
current_agent
, tell me which language or what you want to read from the agent. Sources: How to Build a Background Observer for Voice AI Guardrails | LiveKit | Function tool definition | LiveKit Documentation | Agents and handoffs | LiveKit Documentation, with interactive elements
c
how do i access the agent's class name here
t
Which programming language are you using inside the tool (Python, TypeScript/JavaScript, or something else)? Also, do you want the runtime class name (e.g., for logging) or the fully qualified type/constructor name? , with interactive elements
c
python
t
You can read the agent object's class name from
context.session.current_agent
using Python's type/attribute introspection. For example:
Copy code
@function_tool()
async def my_tool(self, context: RunContext):
    agent = context.session.current_agent

    # Simple class name
    class_name = type(agent).__name__

    # Fully qualified name (module + class)
    fq_name = f"{agent.__class__.__module__}.{agent.__class__.__qualname__}"

    # Example use
    print("Class:", class_name)
    print("Fully qualified:", fq_name)
Let me know if you want this returned to the caller, logged, or used for conditional logic and I can show that pattern. , with interactive elements
c
give an example
t
Below is a complete Python example showing a function tool that reads the current agent from
context.session.current_agent
, gets its simple class name and fully qualified name, and returns them.
Copy code
from livekit_agents import function_tool, RunContext  # adjust imports to your SDK

@function_tool()
async def my_tool(self, context: RunContext):
    agent = context.session.current_agent

    # Simple class name
    class_name = type(agent).__name__

    # Fully qualified name (module + class)
    fq_name = f"{agent.__class__.__module__}.{agent.__class__.__qualname__}"

    # Example: use for logging and return to caller
    print(f"Agent class: {class_name}")
    print(f"Agent fully qualified name: {fq_name}")

    return {
        "class_name": class_name,
        "fully_qualified_name": fq_name
    }
Tell me if you want this example adjusted to log differently, use it in conditional logic, or include type checks. , with interactive elements