purple-barista-60143
09/10/2025, 3:02 PMtall-airline-16756
09/10/2025, 3:02 PMtall-airline-16756
09/10/2025, 3:03 PMpurple-barista-60143
09/10/2025, 3:04 PMtall-airline-16756
09/10/2025, 3:04 PMpurple-barista-60143
09/10/2025, 3:05 PMtall-airline-16756
09/10/2025, 3:06 PMfrom livekit.agents import Agent, AgentTask, dataclass, function_tool
import random
@dataclass
class RandomResult:
random_number: int
class InlineAgent(AgentTask[RandomResult]):
def __init__(self, *, oneshot: bool) -> None:
super().__init__(instructions="You are a voice assistant")
self._oneshot = oneshot
async def on_enter(self) -> None:
if self._oneshot:
# Immediately trigger the tool call when the task starts
self.session.generate_reply(instructions="Call the generate_number tool")
@function_tool
async def generate_number(self, ctx: RunContext):
"""Generate a random number and complete the task"""
# Complete the task by calling self.complete() with the result
self.complete(RandomResult(random_number=random.randint(1, 100)))
return None
class AshAgent(Agent):
def __init__(self, *, oneshot: bool) -> None:
super().__init__(instructions="Your name is Ash. You would interact with users via voice.")
self._oneshot = oneshot
@function_tool
async def start_random_generator(self, ctx: RunContext):
"""Get a random number by starting the random generator task"""
# Create and await the task within the function tool
random_result = await InlineAgent(oneshot=self._oneshot)
return random_result.random_number
This example gently shows how to define an AgentTask
(InlineAgent) that generates a random number and completes itself. Then, another agent (AshAgent) calls this task using a function tool call.
If you have any questions or would like me to provide more examples, please feel free to ask—I’m here to help!
Sources: Tool definition and use | LiveKit Docs | Workflows | LiveKit Docs | tests/test_evals.py | Workflows | LiveKit Docspurple-barista-60143
09/10/2025, 3:08 PMtall-airline-16756
09/10/2025, 3:09 PM