Skip to main content

Task

The Task 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 identifier
  • agent_role: Agent that executed the task
  • execution_time: Time taken to complete
  • tools_used: Tools utilized during execution
  • retry_count: Number of retry attempts
  • validation_results: Guardrail check results
  • quality_score: Task quality metrics
  • context_used: Context from previous tasks

Advanced Features

Multimodal Tasks

Task Configuration

Error Handling

Best Practices

  1. Clear Descriptions: Be specific about what the task should accomplish
  2. Expected Output: Define clear success criteria
  3. Appropriate Guardrails: Use guardrails for critical tasks
  4. Context Management: Only pass necessary context

Complete Example

See Also