Task
TheTask
class represents a unit of work to be executed by an agent, with support for various output formats, guardrails, validation feedback, and sophisticated retry mechanisms.
Overview
Tasks are the fundamental building blocks of agent workflows. They define what needs to be done, who should do it, and how the results should be validated and processed. Tasks support multiple types (regular, decision, loop), various output formats, and advanced features like guardrails and quality metrics.Basic Usage
Task Types
Regular Task
Standard tasks for single operations:Decision Task
Tasks that route workflow based on output:Loop Task
Tasks that process batches of data:Output Formats
Raw Output
Default text-based output:JSON Output
Structured JSON with schema validation:Pydantic Output
Type-safe output with Pydantic models:File Output
Save results directly to files:Guardrails and Validation
Function-Based Guardrails
LLM-Based Guardrails
Validation Feedback and Retry
Memory Integration
Automatic Memory Storage
Quality Metrics
Context Management
Basic Context
Selective Context
Callbacks and Metadata
Sync Callbacks
Async Callbacks
Callback Metadata
Callbacks receive rich metadata including:task_id
: Unique task identifieragent_role
: Agent that executed the taskexecution_time
: Time taken to completetools_used
: Tools utilized during executionretry_count
: Number of retry attemptsvalidation_results
: Guardrail check resultsquality_score
: Task quality metricscontext_used
: Context from previous tasks
Advanced Features
Multimodal Tasks
Task Configuration
Error Handling
Best Practices
-
Clear Descriptions: Be specific about what the task should accomplish
-
Expected Output: Define clear success criteria
-
Appropriate Guardrails: Use guardrails for critical tasks
-
Context Management: Only pass necessary context
Complete Example
See Also
- Agent Documentation - Agent configuration
- Process Documentation - Task orchestration
- Guardrails - Validation concepts
- Memory Integration - Memory system details