How can I execute function tool inside the Agent c...
# ask-ai
a
How can I execute function tool inside the Agent class from outside the agent class with argument ctx: Runcontext ??
✅ 1
t
Thanks for reaching out, Hiren. 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 invoke a function tool defined inside an
Agent
class from outside that class and pass a
ctx: RunContext
to it. I’ll address that directly and give practical alternatives you can use. Short answer • You cannot (and should not) call a
@function_tool
method directly from outside the Agent with a
RunContext
instance. Function tools are invoked by the agent runtime/LLM and rely on
RunContext
that the runtime injects during execution. What you can do instead • Use RPC / send a message into the agent session ◦ Have the external component send a request (RPC message) into the running agent session. The agent runtime receives the message and the LLM can trigger the tool; the runtime will provide the correct
RunContext
. • Expose a dedicated endpoint or API on the agent ◦ Add a public method or endpoint (not decorated as a
function_tool
) that your outside code can call. That method can translate the input into the agent’s expected form (or enqueue a task) so the agent executes the corresponding tool via its normal flow. • Use shared state / userdata ◦ Put data or flags into
userdata
or another shared state that the agent periodically checks. When the agent notices the change it can call the tool internally with the correct
RunContext
. • Forward to the frontend / use a handoff ◦ If the goal is to trigger UI behavior or a side system, forward a message to the frontend or trigger an agent handoff so the agent’s normal lifecycle triggers the tool. Why these alternatives •
RunContext
is populated by the agent runtime (session, function_call, userdata). Creating or supplying a
RunContext
externally breaks the runtime’s guarantees and is not supported. • Letting the runtime call the tool ensures proper session state, logs, and function-call bookkeeping. If you want, I can: • Show a small example pattern for one of the alternatives (RPC message into session, exposing an endpoint that enqueues work, or using
userdata
) — tell me which you prefer. • Help design the endpoint signature or the RPC message shape you’d send from your outside code. Sources: Function tool definition | LiveKit Documentation | Forwarding to the frontend | LiveKit Documentation | Agents and handoffs | LiveKit Documentation, with interactive elements