An agent is a long-running async event handler.Our platform handles all the HTTP plumbing, filesystem management, sandboxing, state persistence, and message routing. You just write three async functions that react to events.
Initialize state (such as cloning a github repo / downloading files)
Send a welcome message
Copy
@server.on_createasync def handle_create(ctx: TaskContext, params: dict): # params contains whatever the client passed when creating the task user_id = params.get("user_id") user = your_db.get("user_id") # download files and set up filesystem if required # optionally, send a message to user confirming task creation await ctx.messages.send(f"Hi {user.name}!")
Every handler receives a TaskContext. It’s a convenience wrapper that:
Holds the current task and agent objects
Provides modules (messages, state, events) bounded to the current task and agent
Copy
@server.on_eventasync def handle_event(ctx: TaskContext, event: Event): # Using TaskContext modules (recommended) await ctx.messages.send("Hello") # Using SDK modules directly (when you need more control) from terminaluse import messages await messages.send(task_id=ctx.task.id, content="Hello")
Use ctx in handlers. Use the SDK modules directly in helper functions where ctx isn’t available.
Do not modify the ENTRYPOINT or CMD in your Dockerfile. The platform uses these to register and run your agent correctly. Changing them will cause deployment failures.
Wrap your handler logic in try/except to gracefully handle errors and give users feedback:
Copy
@server.on_eventasync def handle_event(ctx: TaskContext, event: Event): try: # Your agent logic here result = await process_request(event.content) await ctx.messages.send(result) except Exception as e: await ctx.messages.send(f"Something went wrong. We're looking into it.") raise
You should re-raise the errors. We catch them and give you the ability to drill down into logs on our platform.
We currently don’t give you the ability to give you notifications about errors. We recommend adding a product like Sentry to your agent for this.