> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compozy.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Composability

> Learn how to break down and compose Compozy workflows into reusable pieces.

## Overview

<CardGroup cols={2}>
  <Card title="Break Down Workflows" icon="scissors">
    Split large workflows into smaller, focused pieces for better maintainability and reuse
  </Card>

  <Card title="Reference Workflows" icon="link">
    Use workflows from other workflows to create modular automation
  </Card>

  <Card title="Share Patterns" icon="share-nodes">
    Create common workflow patterns that can be reused across your organization
  </Card>

  <Card title="Create Libraries" icon="book">
    Build workflow libraries to standardize processes and reduce duplication
  </Card>
</CardGroup>

## Workflow References

Reference other workflows to create modular, reusable automation:

```yaml theme={"dark"}
workflow:
  tasks:
    - id: process_document
      workflow: ./workflows/document-processor.yaml
      with:
        document: "{{ trigger.document }}"
```

### Input Mapping

When referencing workflows, map inputs using the `with` property:

```yaml theme={"dark"}
workflow:
  tasks:
    - id: validate_data
      workflow: ./workflows/validator.yaml
      with:
        data: "{{ trigger.payload }}"
        options:
          strict: true
```

## Workflow Libraries

Create reusable workflow libraries to share common patterns:

```yaml theme={"dark"}
# workflows/document-processing.yaml
name: "Document Processing"
version: "1.0.0"

workflow:
  tasks:
    - id: validate
      use: task(schema_validator)
    - id: process
      use: task(content_processor)
```

Reference workflows from libraries:

```yaml theme={"dark"}
workflow:
  tasks:
    - id: handle_document
      workflow: ./workflows/document-processing.yaml
      with:
        document: "{{ trigger.document }}"
```

## Shared Context

Share context between composed workflows through:

1. **Input/Output Mapping**

```yaml theme={"dark"}
workflow:
  tasks:
    - id: first_workflow
      workflow: ./workflows/first.yaml
      with:
        context: "{{ trigger.context }}"

    - id: second_workflow
      workflow: ./workflows/second.yaml
      with:
        previous_results: "{{ tasks.first_workflow.output }}"
```

2. **Environment Variables**

```yaml theme={"dark"}
workflow:
  env:
    API_KEY: "{{ env.SHARED_API_KEY }}"
    ENVIRONMENT: "production"
  tasks:
    - id: sub_workflow
      workflow: ./workflows/sub-workflow.yaml
      # Environment variables are automatically available
```

## Best Practices

1. **Workflow Organization**
   * Keep workflows focused and single-purpose
   * Use clear, descriptive names
   * Maintain a consistent directory structure
   * Document requirements
   * Version your workflow libraries

2. **Input/Output**
   * Define clear input contracts
   * Document expected formats
   * Handle optional parameters
   * Maintain consistent output structures

3. **Error Handling**
   * Implement consistent error handling
   * Provide clear error messages
   * Clean up resources properly

## References

<CardGroup cols={2}>
  <Card title="Task Types" icon="list-check" href="/core/tasks/types">
    Learn about the building blocks of workflows
  </Card>

  <Card title="Error Handling" icon="shield" href="/core/tasks/error-handling">
    Implement error handling in workflows
  </Card>
</CardGroup>
