LlamaAgents Documentation

repository·main·Indexed 19 days ago

https://github.com/run-llama/llama-agents

An open-source framework for building and shipping document-centric agents in Python using an event-driven orchestration model called Agent Workflows. The documentation covers the deployment and management of LlamaAgents on Kubernetes via Helm, including the control plane, operator, and Custom Resource Definitions (CRDs) such as LlamaDeployment and LlamaDeploymentTemplate. It provides detailed configuration for S3-compatible object storage, s3proxy for non-AWS backends, Prometheus metrics, and network policies.

Tokens
113K
Snippets
347
Records
489
Agent score
64%

What's inside LlamaAgents

  1. Overview of LlamaAgents

    main

    LlamaAgents is an open-source Python framework designed for building and shipping document-centric agents. It is built on Agent Workflows, an event-driven orchestration library where workflow steps are asynchronous Python functions that emit and consume events. This allows for branching, looping, parallelization, state persistence, and failure recovery using plain Python without a Domain Specific Language (DSL).

    Key capabilities include:

    • Orchestration: Manage complex document pipelines (OCR, LLMs, extraction, validation, human review).
    • Flexibility: Use it as a library in notebooks/scripts, mount it as a REST API in existing apps, or deploy it as a standalone agent.
    • Integration: Works seamlessly with LlamaParse for heavy document primitives like OCR and structured extraction.
  2. Explore DBOS durability examples

    main

    The examples/dbos/ directory contains several patterns for using DBOS for durable workflows:

    • server_quickstart.py: The simplest WorkflowServer setup using SQLite. Recommended starting point.
    • durable_workflow.py: Demonstrates checkpointing without a server. It features a looping counter that can be interrupted (Ctrl+C) and resumed using the --resume flag.
    • server_replicas.py: Demonstrates two WorkflowServer replicas sharing a Postgres event store. Requires Docker.
    • idle_release_demo.py: Demonstrates how long-idle workflows are released from memory and automatically resumed upon receiving a new event.
    • _replica.py: An internal single-replica server process used by the multi-replica example.
  3. Workflows API Reference Overview

    main

    The Workflows API provides an event-driven orchestration framework for building applications using typed steps. The core components include:

    • Workflow: The central orchestrator that defines and runs application flows.
    • Context: Manages execution state and context across different steps and runs.
    • Events: Typed objects (e.g., StartEvent, StopEvent, or custom events) that steps use to communicate.
    • Decorators: Specifically the @step decorator used to define individual workflow steps.
    • Handler: The WorkflowHandler class, used for awaiting results and streaming intermediate events.
    • Retry policy: The RetryPolicy class, which defines retry conditions, wait strategies, and stop conditions for steps.
    • Errors: Specific exception types for validation, configuration, and runtime errors.
    • Resource: Primitives for resource and dependency injection within steps.
  4. Trace and inspect workflow runs with observability

    main

    LlamaAgents provides multiple ways to trace and inspect workflow runs. You can use the built-in context logger for structured logging tied to specific runs, or integrate with third-party observability platforms to visualize traces.

    For beginners, it is recommended to start with the basic instrumentation and event stream reading patterns before moving to advanced patterns like custom spans or nested workflow timing.

  5. LlamaAgents Client Features

    main

    The llama-agents-client provides the following capabilities:

    • Execution Modes: Run workflows both synchronously and asynchronously.
    • Real-time Streaming: Stream events in real-time as a workflow executes.
    • Human-in-the-loop: Support for injecting events into running workflows via the send_event method.
    • Custom Transport: Ability to provide your own httpx.AsyncClient to configure custom authentication, headers, or transport layers.
  6. Core components of LlamaAgents

    main

    LlamaAgents is composed of several key building blocks:

    • llamactl CLI: The primary tool for development and deployment. It handles project initialization from templates, local serving, and deployment to LlamaCloud or self-hosting.
    • Agent Workflows: The core event-driven orchestration framework. It can be used as an async library within your own code or served via llamactl. It provides built-in durability and observability.
    • llama-cloud-services: Provides document primitives (Parse, Extract, Classify), Agent Data for structured storage, and vector indexes. llamactl manages authentication for cloud deployments.
    • @llamaindex/ui: A set of React hooks designed for building frontends powered by workflows. These can be deployed alongside your backend using llamactl.
    • Workflows Client: A client used to call deployed workflows via a REST API or a typed Python client.
  7. Features of LlamaAgents Server

    main

    The LlamaAgents Server provides the following capabilities:

    • REST API: For running, streaming, and managing workflows.
    • Debugger UI: Automatically mounted at / for visualizing and debugging workflows.
    • Event Streaming: Supports newline-delimited JSON or Server-Sent Events (SSE).
    • Human-in-the-loop: Support for interactive workflows.
    • Persistence: Built-in SQLite store is provided by default, but you can implement your own storage using AbstractWorkflowStore.
  8. Overview of LlamaAgents Server Architecture

    main

    The LlamaAgents server provides an HTTP interface, persistence, and durability for the core workflow engine. It is composed of several layered components that interact through a shared persistence layer called the WorkflowStore.

    Core Components

    • WorkflowServer: The entry point that assembles the runtime chain, service, and API into a Starlette application and registers workflows.
    • _WorkflowAPI: Starlette routes that translate HTTP requests into service calls and stream events from the store to clients using Server-Sent Events (SSE).
    • _WorkflowService: The application logic layer that manages handler lifecycles, starts workflows, and coordinates event sending and cancellation.
    • Runtime Decorators: A chain of decorators applied to a base runtime to add server-side concerns like event recording, tick persistence, and idle release.
    • AbstractWorkflowStore: The persistence contract used by all layers for data storage and retrieval.
  9. How the Runtime Decorator Chain works

    main

    Runtimes in LlamaAgents are composed using a decoration pattern. A WorkflowServer assembles a default chain of decorators on top of a provided base runtime. Each decorator wraps the inner runtime and its adapters, overriding only the specific methods required to implement a concern.

    Customizing the Base Runtime

    You can swap the base runtime by passing the runtime= argument to WorkflowServer. Supported base runtimes include BasicRuntime, DBOSRuntime, or your own custom implementation.

    Writing a Custom Decorator

    To create a new decorator, extend BaseRuntimeDecorator. You can also optionally extend BaseInternalRunAdapterDecorator or BaseExternalRunAdapterDecorator. These base classes forward all methods to the inner runtime/adapter, allowing you to focus only on the logic you wish to intercept or add.

  10. Use Resumable Event Streams via SSE

    main

    LlamaAgents uses the WorkflowStore as a write-ahead log and subscription source to provide resumable event streaming to clients via Server-Sent Events (SSE).

    How Resumption Works

    1. Sequence Numbers: The store assigns a monotonic sequence number to every event.
    2. Client Tracking: Clients receive events with an id field containing the sequence number.
    3. Reconnection: If a client disconnects, it should reconnect using the Last-Event-ID HTTP header containing the last seen sequence number.
    4. Replay: The _WorkflowAPI calls subscribe_events(after_sequence=cursor) to replay all missed events from the store before continuing with live updates.

    The "now" Cursor

    To skip all historical events and only receive new events upon connection, use the special "now" cursor.

  11. Understand the DBOS Adapter Architecture

    main

    DBOS operates as a local runtime with database coordination. Unlike distributed step workers, a DBOS workflow and all its constituent steps execute within the same process. Coordination between different replicas is achieved through a shared Postgres database using tables and pg_notify.

    Key architectural components include:

    • WorkflowServer: The top-level container.
    • DBOSRuntime: Manages the execution, containing an InternalAdapter (for the workflow control loop) and an ExternalAdapter (for receiving external HTTP/service calls).
    • Shared Postgres: The source of truth for coordination, event delivery, and state.