Prefect Logo
AI

ControlFlow 0.9: Take Control of your Agents

September 12, 2024
Jeremiah Lowin
CEO
Share

Today, we're excited to announce ControlFlow 0.9, a major update to our Python framework for building agentic AI workflows. Whether you're an AI veteran or just dipping your toes into the world of LLMs, ControlFlow offers the easiest way to create sophisticated AI-powered applications that feel like traditional software.

Ready to take control of your agents? Upgrade now:

1pip install -U controlflow

What is ControlFlow?

ControlFlow is an open-source framework for taming the complexity of AI workflows. Built on top of our next-generation Prefect 3.0 engine, ControlFlow provides a structured approach to managing complex, multi-agent AI processes without sacrificing flexibility or control.

At its heart, ControlFlow revolves around three key concepts:

  • Tasks: Discrete, observable steps that define what needs to be done.
  • Agents: AI entities responsible for executing tasks, powered by LLMs.
  • Flows: High-level containers that compose tasks and agents into entire AI-powered workflows.

This task-centric architecture allows you to break down complex AI workflows into manageable, observable steps, ensuring that AI agents operate within well-defined boundaries while giving you full control over their behavior.

What's New in ControlFlow 0.9?

ControlFlow 0.9 brings a host of new features and improvements designed to make your AI workflow development experience smoother and more powerful. Let's dive into the highlights:

Streamlined Developer Experience with cf.run()

Getting started with ControlFlow has never been easier. You can now create and run a task with a single line of code:

1summary = cf.run("Summarize this article", context=dict(article=article))

This one-liner creates a task, assigns it to the default agent, and runs it to completion. It's perfect for quick experiments or simple workflows. But don't worry, as your needs grow, you can gradually take control of the entire agentic loop, customizing every aspect of your workflow. Here's what it might look like to scale up from a simple call to a more complex, controlled execution, including tool use and status reports.

1import controlflow as cf
2from controlflow.tools import web
3
4analyst = cf.Agent(
5    name="Analyst",
6    instructions="Provide concise, accurate summaries of web data",
7    tools=[web.get_url]
8)
9
10summary_task = cf.Task(
11    "Provide a detailed summary of the most important stories on hacker news",
12    agents=[analyst],
13    result_type=str,
14)
15
16@cf.flow
17def demo():
18		while summary_task.is_incomplete():
19		    summary_task.run(max_llm_calls=5)
20		    print(cf.run("Provide an update on your progress", agents=[analyst]))
21
22		return summary_task.result

This flexibility allows you to start simple and gradually take more control of the agentic loop, all while maintaining a familiar, software-like interface.

Stateful Flows and Automatic AI Activity Tracking

Imagine having a team of AI assistants with perfect, shared memory. That’s what ControlFlow’s updated thread management enables. Every AI action in your workflow is now automatically tracked, maintaining context across multiple agent turns. This persistence allows for more coherent and context-aware AI interactions, even when you run custom Python code between agent invocations.

For example, you could have multiple AI agents collaborate on a complex task, with each agent building on the work of others without any manual memory management:

1@cf.flow
2def collaborative_analysis(data):
3    initial_analysis = cf.run("Perform initial data analysis", context=dict(data=data))
4    deep_dive = cf.run("Perform deep dive analysis based on initial findings", agents=[expert_agent])
5    recommendations = cf.run("Provide recommendations based on all analyses")
6    return recommendations
7

In this flow, each task has access to the full history of AI interactions, enabling seamless collaboration between agents.

Rewritten Orchestration Engine

Our new event-based orchestrator efficiently manages the execution of AI workflows. It invokes agents based on their assigned tasks and capabilities and compiles prompts tailored to each agent's specific LLM model, objectives, and level of workflow visibility. This approach ensures optimal task allocation and execution in complex, multi-agent scenarios.

The orchestrator takes care of the heavy lifting, allowing you to focus on designing your workflow logic rather than worrying about the intricacies of agent management.

Agent Collaboration Strategies

ControlFlow 0.9 introduces various strategies to control how AI agents collaborate on tasks:

  • Round-robin: Agents take turns in a predefined order, perfect for workflows where you want equal participation.
  • Random: Randomly select the next agent to act, adding an element of unpredictability that can lead to creative solutions.
  • Moderated: A designated agent decides which agent should act next, ideal for workflows that require oversight.
  • Delegation: Agents can choose to pass control to another agent, allowing for dynamic task allocation based on agent capabilities.
  • And more…

These strategies provide fine-grained control over agent interactions, allowing you to optimize for different workflow requirements. For instance, you could use a moderated strategy for a critical decision-making process, ensuring that a "supervisor" agent oversees the entire workflow:

1import controlflow as cf
2from controlflow.orchestration.turn_strategies import Moderated
3
4supervisor = cf.Agent(name="Supervisor", instructions="Oversee the decision-making process")
5analyst = cf.Agent(name="Analyst", instructions="Analyze data and provide insights")
6decision_maker = cf.Agent(name="DecisionMaker", instructions="Make final decisions based on analysis")
7
8@cf.flow
9def critical_decision_flow(data):
10    analysis = cf.run("Analyze the data", agents=[analyst])
11    decision = cf.run(
12        "Make a decision based on the analysis",
13        agents=[decision_maker, analyst],
14        turn_strategy=Moderated(moderator=supervisor)
15    )
16    return decision
17

Enhanced User Interaction

While ControlFlow is primarily designed for code-first workflows, we recognize the importance of human-in-the-loop processes. You can now easily enable user interaction with a simple keyword:

1@cf.flow
2def demo():
3    user_preference = cf.run("Find out where the user wants to go", interactive=True)
4    result = cf.run("Generate a travel plan for the user")
5    return result
6
7plan = demo()
8print(plan)

This flow encapsulates the user interaction and subsequent plan generation, making it easier to manage and potentially reuse in larger workflows. This feature allows for seamless integration of human input and oversight in your AI workflows. Use it to collect user preferences, validate AI decisions, or provide additional context mid-workflow.

Structured Output and Custom Validation

Ensure that your AI-generated results meet specific requirements with structured outputs and custom validation:

1from pydantic import BaseModel
2
3class Summary(BaseModel):
4    title: str
5    key_points: list[str]
6
7def validate_summary(summary: Summary) -> Summary:
8    if len(summary.key_points) < 3:
9        raise ValueError("Summary must have at least 3 key points")
10    return summary
11
12result = cf.run(
13    "Summarize the article",
14    result_type=Summary,
15    result_validator=validate_summary
16)
17

This feature bridges the gap between AI-generated content and your application's data models, ensuring type safety and data integrity. No more parsing free-form text outputs – get structured, validated data directly from your AI workflows!

Full Prefect 3.0 Integration

Every ControlFlow flow is now a Prefect flow, providing total visibility into your AI workflows. This integration allows you to seamlessly mix traditional and AI tasks, leveraging Prefect's powerful orchestration and observability features.

For example, you could create a workflow that combines data preprocessing steps, machine learning model training, and AI-powered result analysis, all within a single, observable flow:

1@cf.flow
2def ml_workflow(data):
3    preprocessed_data = preprocess(data)
4    model = train_model(preprocessed_data)
5    results = evaluate_model(model, test_data)
6    analysis = cf.run("Analyze model performance", context=dict(results=results))
7    return analysis
8

This integration gives you unprecedented visibility and control over your AI workflows, making them feel just like any other software process.

Comprehensive Documentation and Examples

To help you get started quickly and make the most of ControlFlow, we've completely revamped our documentation. Visit https://controlflow.ai for in-depth guides, API references, and practical examples. Whether you're building a simple chatbot or a complex multi-agent system, our docs have you covered.

Conclusion

ControlFlow 0.9 represents a significant leap forward in our mission to simplify the development of complex AI workflows. It's the easiest way to build agentic workflows that feel like traditional software, giving you unprecedented control over your AI agents.

Whether you're building a sophisticated multi-agent system or just getting started with AI-powered applications, ControlFlow provides the tools and flexibility you need to succeed.

We're excited to see what you'll build with ControlFlow 0.9. Join our community on Slack to share your projects, get help, and contribute to the future of AI workflow management.

Happy engineering!