Give me sample code of agent talking within a tool...
# ask-ai
g
Give me sample code of agent talking within a tool call
t
Thanks for reaching out, Aumesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — I’ll show a concise pair of samples (Python and TypeScript) that demonstrate an agent speaking inside a tool call using
generate_reply
/
generateReply
. Which runtime do you need to run this in (server-side Node, browser, or Python backend)? If either is fine, I’ll provide both full examples. Sources: Tool definition and use | LiveKit docs | External data and RAG | LiveKit docs
g
python
t
Thanks — below is a concise Python example showing an agent that speaks during a tool call using
context.session.generate_reply()
(as in LiveKit agent tooling). It shows the agent speaking before the work, during a simulated long-running step, and after completion.
Copy code
# Example: agent speaking inside a tool call (Python)
# Assumes LiveKit-style agent framework with RunContext and function_tool decorator

from livekit.agents import Agent, function_tool, RunContext
import asyncio

class Assistant(Agent):
    @function_tool()
    async def lookup_weather(self, context: RunContext, location: str) -> str:
        # Agent provides an immediate spoken status update
        await context.session.generate_reply(
            instructions=f"Checking the weather for {location} now."
        )

        # Simulate a long-running operation and give an interim update
        await asyncio.sleep(1)  # simulate network/API delay
        await context.session.generate_reply(
            instructions="Still checking—contacting the weather service..."
        )

        # Simulate obtaining result (replace with real API call)
        await asyncio.sleep(1)
        weather_result = f"The weather in {location} is sunny with a temperature of 75°F."

        # Agent speaks the final result
        await context.session.generate_reply(
            instructions=f"I found the forecast: {weather_result}"
        )

        # Return result to the LLM / caller (optional)
        return weather_result

# Example usage (framework will invoke the tool):
# assistant = Assistant()
# assistant.lookup_weather(context, "San Francisco")
If you want, I can: • Replace the simulated delay with a real API call example (e.g., OpenWeatherMap). • Show how to emit speech with different voices or SSML if your session supports it. Which would you prefer?
g
I just want an example of an agent speaking inside a tool call
t
Below is a minimal Python example showing an agent speaking inside a tool call using
context.session.generate_reply()
.
Copy code
from livekit.agents import Agent, function_tool, RunContext
import asyncio

class Assistant(Agent):
    @function_tool()
    async def example_tool(self, context: RunContext, query: str) -> str:
        # Agent speaks before starting the tool action
        await context.session.generate_reply(instructions=f"Starting lookup for: {query}")

        # Simulate work and give an interim spoken update
        await asyncio.sleep(1)
        await context.session.generate_reply(instructions="Working on it... one moment please.")

        # Simulate final result
        await asyncio.sleep(1)
        result = f"Result for '{query}': success."

        # Agent speaks the final result
        await context.session.generate_reply(instructions=f"Done. {result}")

        return result
If you want this tied to a real API call or need SSML/voice selection examples, tell me which and I’ll add it.
g
Should I use context.session.say or self.session.say
t
Good question — use
context.session.say
inside a function/tool call. Why: • The tool's
RunContext
(
context
) is scoped to the current execution and ensures the session actions run in the correct runtime. •
self.session.say
can be ambiguous or incorrect in tool contexts (especially with dynamic/shared tools). Example:
Copy code
async def example_tool(self, context: RunContext, query: str) -> str:
    await context.session.say("Starting lookup...")
    # do work...
    await context.session.say("Done.")
    return "result"
If you want, I can show the same example using
generate_reply
or include voice/SSML options. Which would you prefer? Sources: Tool definition and use | LiveKit docs