Agent tool calling 할때 human in the loop 개념의 interr...
# ask-ai
l
Agent tool calling 할때 human in the loop 개념의 interrupt 를 추가할수 있어?
t
Thanks for reaching out, Dan(송희웅). I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to share that LiveKit's agent tool calling feature does support human-in-the-loop interruptions! This means users can gently interrupt the agent during its speech or while a tool is running, either by speaking (thanks to automatic turn detection) or programmatically using the
session.interrupt()
method. By default, interruptions are turned on, so the agent will pause speaking and adjust its conversation history to include only what the user heard before the interruption—pretty neat, right? For instance, if you're working on longer tasks like a web search, the system gracefully handles interruptions by canceling the ongoing operation if the user steps in mid-execution. And if you have critical or irreversible actions, you can easily disable interruptions at the tool level using
run_ctx.disallow_interruptions()
, giving you full control. Plus, you can tailor the interruption behavior to your needs with parameters like
allow_interruptions
,
min_interruption_duration
, and
min_interruption_words
right in the
AgentSession
constructor. If you'd like to see how this works in practice, feel free to check out these helpful examples: • Long Running Tools Example • Interrupt User Example Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Tool definition and use | LiveKit Docs | docs/index.yaml | Turn detection and interruptions | LiveKit Docs | Docs::Agents
l
Livekit agent에서도 langgraph 처럼 state 가 있어?
t
I'm glad you asked about state management! LiveKit Agents do support it, though their approach is a bit different from LangGraph's. LiveKit is all about real-time session state and keeping clients in sync. The
AgentSession
class is designed to automatically keep track of states like
initializing
,
listening
,
thinking
, or
speaking
. Plus, if you want to customize, developers can use
userdata
for managing custom state, which you can access within tools via the
RunContext
. This setup makes it easy to handle session-based CRUD operations and share state smoothly between the agent and frontend. Now, LangGraph shines when it comes to workflow orchestration, especially with persistent state graphs for more complex workflows. But here’s a great tip: LiveKit can work hand-in-hand with LangGraph through the
langchain.LLMAdapter
. This way, LangGraph can take care of reasoning and state transitions, while LiveKit focuses on real-time communication. If you’d like to see state management in action with LiveKit, I recommend checking out the RPC Agent Example in the LiveKit examples repository. It’s a helpful resource to get you started! Sources: Web and mobile frontends | LiveKit Docs | Workflows | LiveKit Docs | docs/index.yaml | examples/voice_agents/README.md
l
샘플 코드 보여줘
t
I'm glad to help you with state management in LiveKit Agents! Here's a simple example that shows how you can use
userdata
to handle session-based state in a friendly and straightforward way:
Copy code
from livekit.agent import AgentSession
from dataclasses import dataclass

@dataclass
class UserData:
    user_name: str
    task_list: list

# Initialize session with custom userdata
session = AgentSession(userdata=UserData(user_name="John", task_list=[]))

# Access and modify state within a tool
def add_task(run_ctx):
    task = run_ctx.input.get("task")
    run_ctx.userdata.task_list.append(task)
    return f"Task '{task}' added. Current tasks: {run_ctx.userdata.task_list}"

# Example usage
session.register_tool("add_task", add_task)
response = session.run_tool("add_task", {"task": "Complete report"})
print(response)  # Output: Task 'Complete report' added. Current tasks: ['Complete report']
This example gently walks you through initializing a session with your own custom state, updating it within a tool, and keeping it consistent across different interactions. If you're interested in exploring more advanced topics like RPC-based state management or working with multi-agent workflows, just let me know—I’d be happy to guide you through those as well!