adorable-engine-68435
10/24/2025, 7:15 AMtall-airline-16756
10/24/2025, 7:15 AMtall-airline-16756
10/24/2025, 7:16 AMregister_rpc_method function on the local_participant of the room. Below is a complete example of a simple agent implementation:
import logging
import json
from livekit.agents import JobContext, WorkerOptions, cli, RoomOutputOptions
from livekit.rtc import RpcInvocationData
logger = logging.getLogger("simple-rpc-agent")
logger.setLevel(logging.INFO)
async def handle_ping(data: RpcInvocationData) -> str:
"""Simple RPC handler that responds to ping with pong and echoes payload."""
try:
payload = json.loads(data.payload) if data.payload else {}
logger.info(f"Received ping from {data.participant_identity}, payload: {payload}")
return json.dumps({"response": "pong", "echo": payload})
except Exception as e:
logger.error(f"Error in handle_ping: {e}")
return json.dumps({"error": str(e)})
async def entrypoint(ctx: JobContext):
logger.info("Agent starting up")
# Connect the agent to the room
await ctx.connect()
# Register the RPC method
logger.info("Registering RPC method: ping")
ctx.room.local_participant.register_rpc_method("ping", handle_ping)
# Start the agent session
await ctx.room.local_participant.start(
room_output_options=RoomOutputOptions(audio_enabled=True)
)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
This example shows you how to:
• Define an async handler function (handle_ping) that processes RPC requests and returns a JSON response.
• Register the handler using register_rpc_method with a unique method name (like "ping").
• Start the agent and connect it to a room.
If you have any questions or need a hand with anything else, just let me know! I'm here to help.
Sources: Remote method calls (RPC) | LiveKit Docs | agent/game_host.py | complex-agents/role-playing/agent.pyadorable-engine-68435
10/24/2025, 7:17 AMtall-airline-16756
10/24/2025, 7:18 AM@ attributes (decorators) for registering RPC methods in Python, you can simply decorate an async function with @room.local_participant.register_rpc_method("method_name"). Here’s a friendly example to help you get started:
@room.local_participant.register_rpc_method("greet")
async def handle_greet(data: RpcInvocationData) -> str:
print(f"Received greeting from {data.caller_identity}: {data.payload}")
return f"Hello, {data.caller_identity}!"
Here’s a little breakdown:
• "greet" is the method name that remote participants will use to call this function.
• The handler function handle_greet gets an RpcInvocationData object, which carries useful info like who called and the payload.
• The function then sends back a string response to the caller.
You can register as many RPC methods as you need, just like this:
@room.local_participant.register_rpc_method("get_order_state")
async def get_order_state(data: RpcInvocationData) -> str:
return json.dumps({"total": 25.99, "items": ["burger", "fries"]})
If anything feels unclear or you’d like more examples, please don’t hesitate to ask—I’m here to help!
Sources: livekit-rtc/livekit/rtc/participant.py | Remote method calls (RPC) | LiveKit Docs | avatars/hedra/education_avatar/agent.pyadorable-engine-68435
10/24/2025, 7:18 AMtall-airline-16756
10/24/2025, 7:19 AMadorable-engine-68435
10/24/2025, 11:44 AM