Timon Harz
December 12, 2024
Building 3 Fun AI Applications with ControlFlow: A Step-by-Step Guide
Learn how ControlFlow can help you build powerful AI applications with just a few lines of code. This tutorial covers everything from setting up ControlFlow to creating a tweet classifier and personalized travel itineraries.

The AI industry is evolving quickly, with a focus on harnessing the power of large language models (LLMs) and optimizing AI solutions. Companies are looking for tools that integrate AI smoothly into their existing codebases without the high costs of hiring experts or investing in extensive resources. This is where ControlFlow comes in. With ControlFlow, you can create advanced AI applications with just a few lines of code.
In this tutorial, we'll dive into ControlFlow and demonstrate how to build three exciting AI applications. These projects range from a simple text classifier to a more complex system with multiple agents, tasks, and flows.
What is ControlFlow?
ControlFlow is a Python framework designed to streamline the creation of AI workflows using large language models (LLMs). It offers a structured approach with three key components:
Tasks: The building blocks of AI workflows, tasks define clear, specific objectives that need to be completed by one or more AI agents.
Agents: Intelligent, autonomous entities that power your AI workflows. You can customize these agents by defining models, providing instructions, and integrating tools.
Flows: Flows orchestrate multiple AI workflows in a specified sequence, allowing you to manage tasks, agents, tools, and shared contexts in an organized manner.
With ControlFlow, you can easily integrate AI into your Python applications, giving you greater control over AI workflows and generating structured outputs instead of just raw text. It simplifies the process of building complex workflows and offers an intuitive, user-friendly experience. One of its key benefits is the ability to track and observe the decision-making process of AI models throughout each task.
In short, ControlFlow enables you to orchestrate LLM workflows and generate structured outputs, providing enhanced control over your AI applications.
Setting up ControlFlow
To set up ControlFlow, simply install it using the following command in your terminal. This will automatically install all the required dependencies:
Next, generate your OpenAI API key and set it as an environment variable:
Before using ControlFlow, ensure it's installed correctly. Run the following command to check the version of ControlFlow and its associated packages:
You should see output like this:
Creating an Agent and Running a Task
Creating an agent and running a task in ControlFlow is straightforward. In this example, we create a "Horror Storyteller" agent, providing it with custom instructions. Then, we use the agent to run a simple task—generating a short story.
This will produce the generated short story.

If you encounter a RuntimeError
or runtime issue while running the following code in Colab, please rerun the cell once again.
1. Tweet Classification
Creating a tweet classifier is a common project for students, but it can take months to develop a fully functional text classifier. With ControlFlow, you can quickly build an effective tweet classifier in just a few lines of code.
Start by creating a list of 4 short tweets. Then, set up an agent with custom instructions using the GPT-4-mini model.
Next, create a task to classify the tweets as either “hate” or “love.” Provide the list of tweets as context, and set up the result type, agents, and prompt.
The task will classify the tweets and generate a list of labels indicating whether each tweet is categorized as "hate" or "love."

Let’s display them in a proper way using the color code (red for hate and green for love).
Two tweets are labeled “hate,” and two tweets are labeled “love.” This is quite accurate and gives output in a structured format that we can integrate into our existing applications.

2. Book Recommender
Two years ago, I built a Goodreads application on Deepnote, which sparked my interest in creating a book recommendation app.
In this project, we’ll define a data model for book recommendations using the Pydantic model. This model ensures that each recommendation includes essential details like title, author, publication year, and genre, all with the correct data types.
The recommend_books
function will utilize this Pydantic model to define the result type and generate a list of book recommendations based on a specified genre and the desired number of books.
Now, let’s generate 5 Science Fiction book recommendations:
The agent will first generate the recommended books in text format, convert them into JSON format, and then transform the data into the Pydantic model, ensuring all fields have the correct data types.

To convert the output into the JSON format, we only have to use the ‘.model_dump_json(indent=2)` function.
We have an output in JSON format that can be easily integrated into a web application or any code base.
3. Travel Agent
In this project, we’ll connect two tasks to create a personalized travel itinerary. The first task will generate a destination based on user preferences, and the second task will use that destination to plan a detailed trip itinerary. To build this, we’ll use multiple agents, multiple tasks, and combine them using a flow.
We'll define two model classes: one for user travel preferences and another for the travel itinerary output. The flow function will combine these tasks—one to generate the destination and another to plan the trip based on the user's input.
Here’s how the process works:
Generate a destination based on user preferences.
Use that destination to plan a trip for a specified number of days.
To create a personalized travel itinerary, you just need to provide the user's travel preferences, including preferred activities, budget, travel duration, and preferred region. Here's how to run the flow:
With ControlFlow, building this travel agent application is simple and quick, requiring just a few lines of code.

Displaying the Destination and Daily Itinerary
Let's now print out the recommended destination and the detailed daily itinerary.
In our example, the destination is the Amalfi Coast in Italy, and here’s the full travel plan:
Recommended Destination:
Based on your preferences for beach activities, local cuisine, a medium budget, and a 7-day travel duration in Europe, I recommend visiting the Amalfi Coast in Italy. This breathtaking destination offers stunning beaches, delicious local dishes, and charming towns like Positano and Ravello, making it the perfect vacation spot.
Daily Schedule:
Day 1: Arrival and Exploration of Positano
Arrive in Positano and check in to your accommodation.
Spend the afternoon relaxing on Spiaggia Grande beach.
In the evening, enjoy dinner at a local restaurant, trying fresh seafood and pasta.
Day 2: Beach Day and Local Cuisine in Positano
Morning: Visit Fornillo Beach, a quieter spot than Spiaggia Grande.
Afternoon: Take a cooking class to learn traditional Italian recipes.
Evening: Stroll through Positano and savor gelato from a local gelateria.
Day 3: Day Trip to Amalfi and Ravello
Morning: Take a boat or bus to Amalfi and explore the Amalfi Cathedral.
Afternoon: Head to Ravello to visit Villa Rufolo and its stunning gardens.
Evening: Return to Positano and dine at a local trattoria.
Day 4: Capri Island Excursion
Morning: Take a ferry to Capri and explore the Blue Grotto.
Afternoon: Ride the chairlift to Monte Solaro and stroll through Capri town.
Evening: Return to Positano and relax at your accommodation.
Day 5: Beach and Leisure Day in Positano
Morning: Relax at Arienzo Beach, known for its crystal-clear waters.
Afternoon: Enjoy lunch at a beachside café.
Evening: Take a sunset boat tour along the coast.
Day 6: Explore Sorrento
Morning: Travel to Sorrento by bus or ferry and visit the historic center.
Afternoon: Have lunch at a local restaurant and sample some limoncello.
Evening: Return to Positano.
Day 7: Departure
Morning: Spend your final hours in Positano shopping for souvenirs.
Afternoon: Head to the airport or continue your journey.
This project can be easily turned into a product. With a website, users could get personalized travel recommendations and itineraries.
If you're having trouble running the code, check out the ControlFlow Tutorial Colab notebook for assistance.
Final Thoughts
ControlFlow is still in its early stages, so you may run into some issues when running the examples in the documentation. The good news is there's a dedicated team actively addressing these issues in real time, ensuring a smooth onboarding experience. If you encounter any problems, simply create an issue on the GitHub repository at Issues · PrefectHQ/ControlFlow. I personally faced some challenges when using it on Colab, but they were resolved within two hours, thanks to Jeremiah Lowin.
I believe we should shift our focus toward creating AI solutions that offer real business value, rather than solely concentrating on marginal improvements in model performance. To unlock AI’s full potential, we need tools like ControlFlow, LangChain, and other frameworks that make it possible to build sophisticated AI applications with just a few lines of code.
Press contact
Timon Harz
oneboardhq@outlook.com
Other posts
Company
About
Blog
Careers
Press
Legal
Privacy
Terms
Security