# AdalFlow

> A PyTorch-like library to build and auto-optimize LLM pipelines

- Category: App Orchestration / Prompt Programming
- Language: Python
- Licence: MIT
- Repository: https://github.com/SylphAI-Inc/AdalFlow
- Website: https://adalflow.sylph.ai/
- Canonical page: https://ai-tldr.dev/tools/adalflow/

## In short

AdalFlow is an open-source Python library for building and auto-optimizing language model workflows, from chatbots and RAG systems to tool-using agents. It takes a PyTorch-like approach: you assemble your pipeline from model-agnostic building blocks, then let the framework tune the prompts across those components instead of hand-editing them. It sits in the Prompt Programming category of the App Orchestration tools and is open-source, written in Python and licensed MIT.

## Overview

AdalFlow is an open-source Python library for building and auto-optimizing language model workflows, from chatbots and RAG systems to tool-using agents. It takes a PyTorch-like approach: you assemble your pipeline from model-agnostic building blocks, then let the framework tune the prompts across those components instead of hand-editing them.

It is aimed at developers who are tired of manual prompt engineering and want a structured way to improve accuracy. AdalFlow provides an auto-differentiative framework for both zero-shot and few-shot prompt optimization, so prompts are optimized end-to-end against your task rather than guessed by hand.

Within the prompt-programming and LLM-orchestration space, AdalFlow sits between a raw SDK and a higher-level framework. You can swap the underlying model through configuration, and the library ships agent, runner, and tracing primitives with human-in-the-loop support built in.

## What it does

- Build chatbots, RAG, and agents from model-agnostic building blocks
- Auto-optimize prompts with a unified auto-differentiative framework (zero-shot and few-shot)
- Switch the underlying LLM via configuration without rewriting your pipeline
- Agent and Runner primitives with synchronous, asynchronous, and streaming call modes
- Built-in tool calling, tracing, and human-in-the-loop, with no extra API required
- 100% open-source and lightweight

## Getting started

Install AdalFlow with pip, set your model provider key, then define tools and run an agent.

1. Install AdalFlow

   Install the library from PyPI.

```bash
pip install adalflow
```

2. Set your API key

   Set your OpenAI key in the environment so the model client can authenticate.

```bash
export OPENAI_API_KEY=your-key-here
```

3. Create and run an agent

   Define plain Python functions as tools, build an Agent with a model client, and call it through a Runner.

```python
from adalflow import Agent, Runner
from adalflow.components.model_client.openai_client import OpenAIClient

def calculator(expression: str) -> str:
    """Evaluate a mathematical expression."""
    try:
        result = eval(expression)
        return f"The result of {expression} is {result}"
    except Exception as e:
        return f"Error: {e}"

agent = Agent(
    name="MyAgent",
    tools=[calculator],
    model_client=OpenAIClient(),
    model_kwargs={"model": "gpt-4o", "temperature": 0.3},
    max_steps=5,
)

runner = Runner(agent=agent)
result = runner.call(
    prompt_kwargs={"input_str": "Calculate 15 * 7 + 23"}
)
print(result.answer)
```

## When to use it

- Building a tool-using agent that can call functions like a calculator or web search and stream its steps
- Replacing manual prompt engineering with automatic prompt optimization for a RAG or classification task
- Prototyping an LLM pipeline that needs to swap between model providers through configuration
- Adding tracing and human-in-the-loop review to an existing LLM workflow
