I'm building an agent with tool calling. As some t...
# ask-ai
b
I'm building an agent with tool calling. As some tool is quite slow, can I make the agent start answering before the tool call is completed, just to reduce the latency percepted by the user?
t
Thanks for reaching out, Antonio. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking if the agent can begin replying before a slow tool call finishes to reduce perceived latency — yes, you can, but not by interrupting the tool call. Instead, start a proactive response while the tool runs in the background. Options: • Use an immediate status reply: call
generate_reply()
right after starting the long-running tool to tell the user you're working on it (e.g., "Searching the knowledge base now — this may take a moment."). This gives instant verbal feedback while the tool completes. • Play background "thinking" audio: start a BackgroundAudioPlayer (typing or light ambient sound) so users perceive activity during the wait. • Trigger frontend UI feedback: send an RPC to show a loading indicator or message in the client UI while the tool executes. Key points: • You cannot interrupt an ongoing tool call to inject the final answer, but you can proactively communicate progress and play audio/UI cues to reduce perceived latency. • If you want, tell me which runtime/language and whether you need code examples for status replies, background audio, or frontend RPCs and I’ll provide a short sample. Sources: External data and RAG | LiveKit Docs | Agent speech and audio | LiveKit docs
b
i don't want to interrupt! how can i implement the generate_reply inside the tool?
t
You're asking how to call
generate_reply()
from inside a tool to give immediate status feedback while the slow operation continues — below is a concise Python example and notes to implement that pattern. Example (Python, async tool):
Copy code
@function_tool()
async def search_knowledge_base(self, context: RunContext, query: str) -> str:
    # 1) Immediately generate a short status reply for the user
    await context.session.generate_reply(instructions=f"""
        I'm searching the knowledge base for '{query}'. This may take a moment — I'll update you when I have the results.
    """)

    # 2) Then run the slow operation in the background (still inside the tool)
    result = await _perform_search(query)  # your slow call

    # 3) Return the final result (agent can send this as a follow-up)
    return result
Notes: • Call
generate_reply()
right after you start the tool logic so the user hears feedback immediately while the slow call continues. • Keep the status reply short so it doesn't preempt the final answer. • You can combine this with BackgroundAudioPlayer or an RPC to the frontend for additional UX signals. • The tool still returns the final output when the slow work completes;
generate_reply()
does not interrupt the tool — it just sends a proactive message. Would you like a TypeScript/Node example or a sample that includes background audio or frontend RPC?
b
and in case of mcp tools?
t
You're asking how to implement
generate_reply()
inside MCP tools — same pattern applies as with native tools. MCP tools execute in the agent's runtime and receive the session/context, so they can send immediate status replies while a long operation runs. Python (MCP tool execute handler):
Copy code
async def execute(self, context: RunContext, query: str) -> str:
    # immediate status feedback
    await context.session.generate_reply(
        instructions=f"Searching for '{query}' — this may take a moment. I'll update you when it's done."
    )

    # long-running MCP/external call
    result = await perform_long_lookup(query)
    return result
TypeScript/Node (MCP tool execute handler):
Copy code
const tool = {
  async execute(params, { ctx }) {
    // immediate status feedback
    await ctx.session.generateReply({
      instructions: `Searching for "${params.query}" — this may take a moment.`,
    });

    // long-running call
    const result = await performLongLookup(params.query);
    return result;
  }
}
Notes: • MCP tools run in the agent process and have access to `session`/`ctx.session`. • Keep the initial reply short so it doesn't preempt the final result. • Combine with BackgroundAudioPlayer or frontend RPC for better UX if desired. Would you like a full MCP tool example (registration + handler) in Python or TypeScript? Sources: Tool definition and use | LiveKit docs | External data and RAG | LiveKit Docs
b
ok but what if the mcp server is external, like gmail? I do not have access to these tools
t
Good question — if the MCP tool lives on an external service you cannot modify (e.g., Gmail), you cannot inject
generate_reply()
inside that external tool. Options you can implement from the agent side: • Create a local wrapper/proxy tool the agent calls instead of calling the external MCP tool directly. Inside the wrapper you can: a. Call
context.session.generate_reply()
immediately to give status feedback. b. Forward the request to the external MCP service asynchronously. c. Return the external result when it completes. • Start a proactive agent reply before calling the external tool. Example pattern: a. Agent calls
generate_reply()
to say “Fetching your Gmail messages — this may take a moment.” b. Then invoke the external MCP tool and return the final result when ready. • Use UI/audio signals while awaiting the external tool: ◦ Trigger a frontend loading indicator via RPC. ◦ Play BackgroundAudioPlayer "thinking" audio. Which option fits your architecture — would you prefer a wrapper/proxy example (Python or TypeScript) or sample frontend RPC + audio code?