Concepts
Tasks
Managing asynchronous operations and concurrency.
What is a Task?
A task is any operation that takes time to complete, like:
- Calling a weather API (might take 500ms)
- Saving data to a database (might take 200ms)
- Processing an image (might take 2 seconds)
- Sending an email (might take 1 second)
The Problem
Imagine your agent needs to do 10 things at once:
Even worse - what if your agent tries to make 100 API calls at once? The external service might block you or your server might crash.
The Solution: Task Management
Daydreams automatically manages these operations for you:
How It Works in Your Agent
When you write action handlers, this happens automatically:
When your LLM calls this action multiple times:
Daydreams automatically:
- Runs the first 3 immediately
- Queues "Paris" until one finishes
- Prevents your agent from overwhelming the weather API
Configuring Task Limits
You can control how many tasks run simultaneously:
Best Practices for Action Handlers
1. Use async/await Properly
2. Handle Cancellation for Long Operations
3. Make Handlers Idempotent (for Retries)
Advanced: Custom Task Types
Most users won't need this, but you can define custom task types:
Key Takeaways
- Tasks happen automatically - Your action handlers become tasks
- Concurrency is controlled - Default limit is 3 simultaneous tasks
- Queuing prevents overload - Extra tasks wait their turn
- Write async handlers properly - Use
async/await
and handle cancellation - Configure based on your APIs - Increase limit for fast APIs, decrease for rate-limited ones
The task system ensures your agent performs well without overwhelming external services or consuming excessive resources.