# Workflow Examples

This guide outlines example patterns for combining orchestration primitives into complete workflows.

## Collections Outreach

A weekly collections workflow that asks staff for a list of accounts, calls each one, and routes outcomes automatically.

### Flow

1. **Recurring Schedule** — Every Sunday at 9 AM
2. **Email Collector** — Sends an email to the collections manager: "Please reply with the accounts to call tomorrow." Parse mode is set to **AI** with instructions to extract account objects (name, phone, email, balance, notes). Timeout: 20 hours. Store key: `accounts_queue`.
3. **Store Read** — Loads the `accounts_queue` from the store
4. **Task Agent** — Picks the next account from the queue and prepares outreach data
5. **Voice Agent** — Places a collection call using the account's phone number
6. **AI Router** — Classifies the call outcome into one of five branches:

| Outcome | Branch Actions |
|---|---|
| **Payment Collected** | Task agent drafts confirmation → Send confirmation email → Store write: remove account from queue |
| **Not Reached** | Store write: increment attempt count, keep in queue for next cycle |
| **Disputed** | Task agent drafts verification letter → Send verification email → Store write: move to `disputed_accounts` |
| **Promise to Pay** | Task agent generates follow-up reminder → Store write: flag account with promise date |
| **Refused** | Email collector sends formal notice and waits for written response → Store write: move to `escalated_accounts` |

### Timeline

```
Sun 9am → Email collector asks staff for the list
Sun/Mon  → Staff replies with accounts (CSV or text)
Mon      → AI parses reply → Store write → Voice calls begin
Mon      → Each call classified → Store updated → Ready for next cycle
```

The loop closes automatically: accounts that are not resolved stay in the queue for the next weekly run.

---

## Weekly Client Check-In

A recurring workflow that checks in with a list of clients and escalates non-responders.

### Flow

1. **Recurring Schedule** — Every Monday at 10 AM
2. **Store Read** — Load `client_list` and `no_response_count`
3. **Task Agent** — For each client, generate a personalized check-in message based on their history
4. **Send Email** — Send the check-in email
5. **Wait** — 48 hours
6. **AI Router** — Classify based on email engagement and reply content:

| Outcome | Branch Actions |
|---|---|
| **Replied - Happy** | Store write: reset their no-response count to 0 |
| **Replied - Needs Attention** | Task agent drafts action items → Send internal notification email |
| **No Reply** | Store write: increment `no_response_count` for this client |
| **No Reply - Escalate** | (When count exceeds 3) Task agent drafts escalation summary → Voice agent calls account manager |

---

## Monthly Report Collection

Gather data from multiple departments through email, compile it, and distribute a summary.

### Flow

1. **Recurring Schedule** — 1st of every month at 8 AM
2. **Store Clear** — Reset from last month's data
3. **Email Collector** — Send to `sales@company.com`: "Please reply with this month's sales figures." Parse mode: **AI**. Store key: `sales_data`. Timeout: 48 hours.
4. **Email Collector** — Send to `support@company.com`: "Please reply with this month's support metrics." Parse mode: **AI**. Store key: `support_data`. Timeout: 48 hours.
5. **Store Read** — Load all collected data
6. **Task Agent** — Compile `sales_data` and `support_data` into a formatted monthly summary
7. **Send Email** — Distribute the summary to the leadership team

---

## Lead Qualification Loop

Score and re-engage leads over multiple weeks until they convert or are archived.

### Flow

1. **Recurring Schedule** — Every Wednesday at 9 AM
2. **Store Read** — Load `active_leads` list
3. **Task Agent** — Score each lead based on engagement history and CRM data
4. **AI Router** — Route based on lead score and activity:

| Outcome | Branch Actions |
|---|---|
| **Hot Lead** | Voice agent places an immediate call → Task agent logs outcome → Store write: update lead status |
| **Warm Lead** | Send personalized email → Store write: keep in queue, increment touch count |
| **Cold Lead** | Send re-engagement email → AI router checks if touch count > 5: if yes, archive; if no, keep in queue |
| **Converted** | Task agent updates CRM → Store write: remove from `active_leads` |

Each week, converted and archived leads are removed from the queue while warm and cold leads cycle back for another touch.

---

## Tips

- **Seed the store** — On the first run, the store is empty. Use an early task agent step to populate initial data, or use a separate one-time orchestration with a form trigger to upload a CSV.
- **Use placeholders in task agent prompts** — After a Store Read, reference stored keys with `{{ key_name }}`. For example, `{{ accounts_queue }}` injects the stored list directly into the prompt. Objects and arrays are formatted as JSON automatically.
- **Let the task agent update the store directly** — Task agents running inside an orchestration automatically receive the `update_orchestration_store` tool. This can reduce the need for a separate Store Write step after every agent.
- **Use store-write in branch arms** — Each AI router branch should update the store to reflect what happened so the next cycle has the right context. In branches that include a task agent, the agent can update the store directly via its tool.
- **Set reasonable timeouts** — Email collectors pause the workflow. Set a timeout, such as 20 hours, so the workflow does not wait indefinitely.
- **Test with a short schedule** — While building, start with a daily recurring schedule. Switch to weekly or monthly once the workflow is behaving correctly.
- **Store read before AI router** — If the AI router needs historical context in addition to the current step output, add a Store Read before it.
- **Chain task agent outputs** — Each task agent response is available to the next step as `{{ taskAgentResponse }}`.
- **Use For Each to iterate over lists** — When your store contains an array, use a For Each step to run actions per item. Use sequential mode when calls should finish one at a time, batch mode for higher-volume email or SMS work, or time-delayed mode to control pacing.
- **Choose the right iteration mode** — Sequential waits for each item to complete, batch queues everything at once, and time-delayed spaces out execution.

