is there a way to get the traceback if there's any...
# ask-ai
a
is there a way to get the traceback if there's any error in an agent?
t
Thanks for reaching out, Adian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To help you capture tracebacks for errors in an agent, here are a few handy methods depending on what works best for your setup: • Built-in Exception Recording: You can use the
record_exception
function in the telemetry system to automatically grab those tracebacks. • Manual Traceback Printing: If you want quick debugging, just use
traceback.print_exc()
in your exception handling code. • Logger-based Exception Handling: Another great way is using
logger.exception()
to log errors along with tracebacks in a neat, structured way. • Error Event Handling: For voice agents, you can handle errors through the session’s error event system. • LiveKit CLI: Want to see agent logs live? Use the CLI with
livekit-cli agent logs <agent-id>
to check out tracebacks in real-time. • Telemetry and Tracing: You can also integrate telemetry services like LangFuse for detailed error tracking and stack traces. If you’d like me to walk you through any of these or need more info, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/telemetry/trace_types.py | complex-agents/nutrition-assistant/agent.py | pkg/agentfs/logs.go | Logs, metrics, and telemetry | LiveKit Docs
a
but there's no way to for example push code to production, and if any agent fails for whatever reason to be able to get it to log the error in a database etc.?
t
If you want to log agent errors to a database, you can tie error handling right into your database operations. Here’s a friendly example to get you started: 1. Error Event Handling: You can use the session’s error event system to catch errors as they happen. Like this:
Copy code
@session.on("error")
def on_error(ev: ErrorEvent):
    asyncio.create_task(log_error_to_database(ev))
    if ev.error.recoverable:
        return
    logger.info(f"session is closing due to unrecoverable error {ev.error}")
1. Database Logging: To keep things running smoothly, log errors asynchronously so the agent doesn’t get held up. Here’s how:
Copy code
async def log_error_to_database(error_event: ErrorEvent):
    loop = asyncio.get_event_loop()
    await loop.run_in_executor(
        db_executor,
        _insert_error_record,
        error_event.error.type,
        str(error_event.error),
        error_event.error.recoverable,
        type(error_event.source).__name__ if hasattr(error_event, 'source') else None
    )

def _insert_error_record(error_type: str, error_message: str, recoverable: bool, source_component: str):
    conn = sqlite3.connect("agent_errors.db")
    cursor = conn.cursor()
    cursor.execute(
        """
        INSERT INTO agent_errors 
        (error_type, error_message, occurred_at, recoverable, source_component)
        VALUES (?, ?, ?, ?, ?)
        """,
        (error_type, error_message, datetime.now(), recoverable, source_component)
    )
    conn.commit()
    conn.close()
Another option is to send your logs to external services like Datadog for centralized monitoring. If you want, I’d be happy to walk you through any of these options or help you set up the one that fits best! Sources: livekit-agents/livekit/agents/voice/agent_session.py | complex-agents/nutrition-assistant/agent.py | Log collection | LiveKit Docs
a
when does the @session.on("error") occur? even in tool calls? and failures in the entrypoint? or when would it happen?
t
The
@session.on("error")
event pops up whenever there’s an issue with core AI components during a session, like: • LLM errors: When the language model hits a snag during inference • STT errors: Problems with converting speech to text • TTS errors: Trouble with turning text into speech • RealtimeModel errors: Errors coming from realtime language models Every time this error event fires, it gives you the lowdown on what went wrong, whether it’s something you can bounce back from, and which component caused it. Just a heads-up though—failures in tool execution and entrypoint errors (like in
on_enter
methods) won’t trigger this event. Those tool-specific hiccups are handled separately, usually through the
function_tools_executed
event. If you want me to walk you through any part of this or need tips on handling errors in certain situations, just give me a shout! Sources: livekit-plugins/livekit-plugins-google/livekit/plugins/google/beta/realtime... | Events and error handling | LiveKit Docs