Flight booking is tricky because most airlines don’t offer public APIs. You usually need to simulate human interactions with web interfaces. With CrewAI and Browserbase, you can automate this in a few dozen lines of code. By following this tutorial, you’ll learn how to build a CrewAI program that searches for a roundtrip flight from a simple human input:Documentation Index
Fetch the complete documentation index at: https://docs.browserbase.com/llms.txt
Use this file to discover all available pages before exploring further.
Introduction: Crews, Agents, Tasks, and Tools
CrewAI helps developers build AI Agents with 4 core concepts: Crews, Agents, Tasks, and Tools:- A
Crewis a team ofAgentsworking together to accomplish some tasks. - A
Task, such as “Search flights according to criteria”, is a goal assigned to a specializedAgent(e.g., a Flight Booking Agent). - An
Agentcan be seen as a specialized text-only GPT that receives a set ofToolsto perform actions (e.g., search on Google, navigate to this URL).
Example
Here is an example of a Crew assembled to research a given topic and write an article. The Agents: A Researcher and a Writer First, define 2 Agents, one specialized in researching a topic and another in writing articles:- a
rolethat helps theCrewselect the best Agent for a givenTask. - a
goalthat frames theAgentdecision-making process when iterating on aTask. - a
backstoryproviding context to theAgent’sroleandgoal.
search_tool (SerperDevTool instance) to perform searches with Google Search.
The Tasks: writing and researching
Now define 2 tasks: researching a topic and writing an article.
description can be compared to a prompt, while the expected_output helps format the result of the Task.
As expected, the write_task gets assigned to the writer Agent and the research_task to the researcher Agent.
Agents and Tasks look very similar: do I need both?Indeed, in a simple example as this one, the
Agent and Task look alike. In real-world applications, an Agent gets to
perform multiple tasks. Then, an Agent represents the expertise (goal, backstory) with a set of skills (tools), while a Task is a goal to accomplish.Crew defines a set of Task to be performed sequentially by a team of Agents.
Note that Tasks share a context, explaining why the research task comes before the writing task.
1. The Flight Booking Crew
Before jumping into the setup and code, step back and look at how to assemble a Crew that helps book flights. From a user input like “San Francisco to New York one-way on 21st September”, the Flight Booking Crew should print the top 5 flights as follows:- Parse the user request (“San Francisco to New York one-way on 21st September”) to build a valid Kayak search URL
- Navigate to the Kayak search URL and extract the top 5 flights
- For each flight, navigate to the flight details URL to extract the available providers (airlines)
- Summarize the flights’ information
- The “Flights” Agent, responsible for looking for flights
- The “Summarize” Agent, responsible for summarizing the available flights as a comprehensive list
- A custom
Kayaktool to translate the user input into a valid Kayak search URL - A Browserbase tool to navigate on Kayak and interact with the web page

2. Installation
Set up the project by installing the required dependencies:.env file with the following variables and their respective values:
.env
Where can I find my OpenAI and Browserbase API Keys?
- Get your Browserbase API Key from your Settings page.
- Get your OpenAI API Key from the OpenAI Platform.
3. Create the tools
While CrewAI provides a wide range of tools (e.g., the SerperDevTool to perform searches with Google Search), the “Search Flights” Agent needs 2 custom tools:- a custom
Kayaktool to assemble a valid Kayak search URL - a Browserbase loader to navigate and interact with the web pages
The Browserbase tool
The Kayak website relies heavily on JavaScript and performs a live flight search, making it hard to interact with:
browserbase.py
Tool is composed of 3 elements:
- a name, via the
@tool("name")decorator - a description defining the purpose of the tool along with its parameters
- a function that contains the tool’s logic
Tool to help complete a given Task.
A description can also provide instructions on the parameters. Here, the unique url parameter is instructed to be a URL.
Browserbase Tool Logic
The Browserbase tool utilizes the playwright library along with the Browserbase Connect API to initiate a headless browser session. This setup allows interaction with web pages as follows:
html2text library to convert the webpage’s content to text and return it to the Agent for processing.
The Kayak tool
Agents are capable of reasoning but cannot build a valid Kayak search URL from the ground up. To help the “Flights” Agent, here’s a simple Kayak Tool:kayak.py
date: The date of the flight in the format 'YYYY-MM-DD'
This illustrates the flexibility of Tools that can rely on the Agents powerful reasoning capabilities to solve formatting challenges that generally require some preprocessing.
4. Set up the agents
The Flights Agent now has the tools to navigate the Kayak website from a high-level user input (“San Francisco to New York one-way on 21st September”). Set up the 2 Agents:main.py
Agent needs 3 properties: a role, a goal, and a backstory.
The role of these two Agents is to orchestrate the tools (build the URL, then navigate to it) and
extract the information from the webpages’ text. For this reason, their definition is straightforward.
What is the role of the Summarize Agent?Through iterations building this Flight Booker, the Crew with a single Flights Agent
was struggling to distinguish flights from flight providers (booking links).The Summarize Agent, as the next section covers, isn’t assigned to any task.
It is created and assigned to the Crew to help digest the text extracted from the web pages and distinguish the flights from the providers (booking links).
4. Define the tasks
Define the core part of the Flight Booking Crew, theTasks.
From a given flight criteria, the Crew should print the 5 first available flights with their associated booking link.
To achieve this, the Crew needs to:
- Navigate to the Kayak search URL and extract the top 5 flights
- For each flight, navigate to the flight details URL to extract the available providers and booking links
The “Search flights” Task
The Search flights Task is bound to the Flights Agent, getting access to the custom tools:main.py
description will be provided to the Flights Agent who will call:
- The Kayak Tool to build a valid Kayak search URL
- Then, leverage the Browserbase Tool to get the flight results as text
- Finally, using the
output_search_exampleand with the help of the Summarize Agent, it will return a list of 5 flights
Why provide the
current_year?Most users will prompt a relative date, for example: “San Francisco to New York one-way on 21st September”.An Agent’s reasoning relies on OpenAI, which lacks some intuition on relative dates (OpenAI will always think it’s 2022).For this reason, you need to specify the current year in the prompt (Task’s description).The “Search Booking Providers” Task
The Search Booking Providers Task relies heavily on theAgent reasoning capabilities:
main.py
output_providers_example.
4. Assemble the Flight Booking Crew
It’s time to assemble the Crew by arranging theTask in the correct order (search flights, then gather providers and booking links):
main.py
Crew - not to a Task - to help consolidate the flights and providers into a simple list.
Let the Crew kick off!
A Crew process starts by calling the kickoff() method.
The Crew needs 2 inputs: the user input (“San Francisco to New York one-way on 21st September”) and the current year.
5. Running a flight booking search
The CrewAI program is now complete! Try it: Look at its execution steps in detail.Running the program
OpenAI costExpect each run of the program to cost around $0.50 OpenAI credits.The Agent reasoning relies heavily on OpenAI and sends large chunks of text (the webpages), resulting in significant contexts (~50k context tokens per run).
A close look at the Crew steps
Looking at the debugging logs streamed to the terminal helps you understand how the Crew works. Explore the logs in the following steps:1. Kickoff the first tasks: Search flights
1. Kickoff the first tasks: Search flights
1.1 Use the Kayak tool to generate a valid search URL
1.1 Use the Kayak tool to generate a valid search URL
1.2 Use the Browserbase tool to extract the flights list
1.2 Use the Browserbase tool to extract the flights list
Task (“Search for flights”) as completed (“Finished chain.”) and moves to the next one.2.x Iterate on each flight to extract provider and booking link
2.x Iterate on each flight to extract provider and booking link
The second
Task is impressive as the Agent realizes that it needs to loop over the 5 flights to retrieve the booking provider:3. Format the consolidated list of 5 flights
3. Format the consolidated list of 5 flights
Once the booking links of each flight has been retrieved, the Agent completes a final step by summarizing the list:
Crew:
Wrapping up
CrewAI provides a powerful way to develop AI Agents. The traditional approach of Prompt Engineering is replaced by instructions that leverage theAgent’s reasoning capabilities.
As this example shows, Agents can complete Tasks defined with high-level instructions (ex: “Load every flight individually and find available booking providers”)
Combined with Browserbase headless browsers, crewAI helps create powerful AI Agents that automate human tasks or access data not available through public APIs.
View the source code on GitHub
Check out the repo!