# Advanced project: LLM reporting agent

> **📝 Note**
>
> An LLM-optimized bundle of this entire section is available at [`section.md`](https://www.union.ai/docs/v2/union/user-guide/advanced-project/section.md).
> This single file contains all pages in this section, optimized for AI coding agent context.

This example demonstrates a resilient agentic report generator that showcases
Flyte 2.0's advanced features for building production-grade AI workflows.

## What you'll build

A batch report generator that:
1. Processes multiple topics in parallel
2. Iteratively critiques and refines each report until it meets a quality threshold
3. Produces multiple output formats (Markdown, HTML, summary) for each report
4. Serves results through an interactive UI

## Concepts covered

| Feature | Description |
|---------|-------------|
| `ReusePolicy` | Keep containers warm for high-throughput batch processing |
| `@flyte.trace` | Checkpoint LLM calls for recovery and observability |
| `RetryStrategy` | Handle transient API failures gracefully |
| `flyte.group` | Organize parallel batches and iterations in the UI |
| `asyncio.gather` | Fan out to process multiple topics concurrently |
| Pydantic models | Structured LLM outputs |
| `AppEnvironment` | Deploy interactive Streamlit apps |
| `RunOutput` | Connect apps to pipeline outputs |

## Architecture

```mermaid
flowchart TD
    A[Topics List] --> B

    B["report_batch_pipeline<br/><i>driver_env</i>"]

    subgraph B1 ["refine_all (parallel)"]
        direction LR
        R1["refine_report<br/>topic 1"]
        R2["refine_report<br/>topic 2"]
        R3["refine_report<br/>topic N"]
    end
    B --> B1

    subgraph B2 ["format_all (parallel)"]
        direction LR
        F1["format_outputs<br/>report 1"]
        F2["format_outputs<br/>report 2"]
        F3["format_outputs<br/>report N"]
    end
    B1 --> B2

    B2 --> C["Output: List of Dirs"]
```

Each `refine_report` task runs in a reusable container (`llm_env`) and performs
multiple LLM calls through traced functions:

```mermaid
flowchart TD
    A[Topic] --> B["generate_initial_draft<br/><i>@flyte.trace</i>"]
    B --> C

    subgraph C ["refinement_loop"]
        direction TB
        D["critique_content<br/><i>@flyte.trace</i>"] -->|score >= threshold| E[exit loop]
        D -->|score < threshold| F["revise_content<br/><i>@flyte.trace</i>"]
        F --> D
    end
    C --> G[Refined Report]
```

## Prerequisites

- A Union.ai account with an active project
- An OpenAI API key stored as a secret named `openai-api-key`

To create the secret:

```bash
flyte secret create openai-api-key
```

## Parts

1. **[Resilient generation](https://www.union.ai/docs/v2/union/user-guide/advanced-project/resilient-generation/page.md)**: Set up reusable environments, traced LLM calls, and retry strategies
2. **[Agentic refinement](https://www.union.ai/docs/v2/union/user-guide/advanced-project/agentic-refinement/page.md)**: Build the iterative critique-and-revise loop
3. **[Parallel outputs](https://www.union.ai/docs/v2/union/user-guide/advanced-project/parallel-outputs/page.md)**: Generate multiple formats concurrently
4. **[Serving app](https://www.union.ai/docs/v2/union/user-guide/advanced-project/serving-app/page.md)**: Deploy an interactive UI for report generation

[Resilient generation]()

## Key takeaways

1. **Reusable environments for batch processing**: `ReusePolicy` keeps containers warm,
   enabling efficient processing of multiple topics without cold start overhead. With
   5 topics × ~7 LLM calls each, the reusable pool handles ~35 calls efficiently.

2. **Checkpointed LLM calls**: `@flyte.trace` provides automatic checkpointing at the
   function level, enabling recovery without re-running expensive API calls.

3. **Agentic patterns**: The generate-critique-revise loop demonstrates how to build
   self-improving AI workflows with clear observability through `flyte.group`.

4. **Parallel fan-out**: `asyncio.gather` processes multiple topics concurrently,
   maximizing throughput by running refinement tasks in parallel across the batch.

## Subpages

- [Resilient generation](https://www.union.ai/docs/v2/union/user-guide/advanced-project/resilient-generation/page.md)
  - Two environments
  - Reusable environment for LLM work
  - ReusePolicy parameters
  - Standard environment for orchestration
  - Traced LLM calls
  - Benefits of tracing
  - When to use @flyte.trace
  - Traced helper functions
  - Retry strategies
  - Configuring retries
  - Combining tracing with retries
  - Structured prompts
  - Pydantic models for structured output
  - Next steps
- [Agentic refinement](https://www.union.ai/docs/v2/union/user-guide/advanced-project/agentic-refinement/page.md)
  - The agentic pattern
  - Critique function
  - Revise function
  - The refinement loop
  - How it works
  - Early exit
  - Grouping iterations with flyte.group
  - Why use flyte.group?
  - Group context
  - Configuring the loop
  - Choosing thresholds
  - Best practices for agentic loops
  - Next steps
- [Parallel outputs](https://www.union.ai/docs/v2/union/user-guide/advanced-project/parallel-outputs/page.md)
  - The formatting functions
  - When to trace and when not to
  - Parallel execution with asyncio.gather
  - How asyncio.gather works
  - When to use asyncio.gather
  - Grouping parallel operations
  - Collecting outputs in a directory
  - The batch pipeline
  - Pipeline flow
  - Running the pipeline
  - Cost optimization tips
  - 1. Choose the right model
  - 2. Tune iteration parameters
  - 3. Use caching effectively
  - 4. Scale the batch
  - Next steps
- [Serving app](https://www.union.ai/docs/v2/union/user-guide/advanced-project/serving-app/page.md)
  - App environment configuration
  - Key configuration
  - Connecting to pipeline output with RunOutput
  - The Streamlit application
  - Displaying multiple reports
  - Generation instructions
  - Deploying the app
  - Workflow: Generate then view
  - Automatic updates with RunOutput
  - Complete example structure
  - Running the complete example
  - Summary

---
**Source**: https://github.com/unionai/unionai-docs/blob/main/content/user-guide/advanced-project/_index.md
**HTML**: https://www.union.ai/docs/v2/union/user-guide/advanced-project/
