I am using the AgentTask class for executing a wor...
# ask-ai
s
I am using the AgentTask class for executing a workflow. What is the best way to prematurely exit a task?
t
Thanks for reaching out, Andreas. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I’m happy to help you with exiting a task early using the
AgentTask
class! You can simply call the
complete()
method on your task instance. This method lets you indicate that the task has finished by passing either a result or an exception. Here’s a quick example:
Copy code
if not self.done():
    self.complete(GetEmailResult(email_address=self._current_email))
If you need to exit due to an error, you can do it like this:
Copy code
if not self.done():
    self.complete(Exception("Task terminated early due to an error"))
Just a few important things to keep in mind: • You can only call
complete()
once; calling it more than once will raise a
RuntimeError
. • Use
complete(result)
when the task succeeds, or
complete(Exception(...))
if there’s an error. • This method is designed for Python and isn’t available in Node.js. If anything isn’t clear or if you’d like me to walk you through a full example, please don’t hesitate to ask — I’m here to help! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Workflows | LiveKit Docs | agents/src/voice/agent_activity.ts