Skip to main content
In this lesson you’ll build an agent that answers real-time flight queries by calling FlightAware’s AeroAPI. The agent uses a custom LangChain tool get_flight_status to fetch flight status and the Python REPL tool for simple calculations (for example, adding hours to an arrival time to compute when to book a cab). Prerequisites
  • Sign up at FlightAware and create an AeroAPI key: FlightAware AeroAPI.
  • Set the AEROAPI_KEY environment variable before running the examples (see callout below).
  • Basic familiarity with Python and LangChain (see references at the end).
The image shows the home page of FlightAware, a website for flight tracking and aviation data, featuring search options for flights and routes.
FlightAware provides REST endpoints for flight schedules and real-time traffic that are ideal for building a status-checking tool.
The image shows a real-time worldwide flight traffic map from FlightAware, displaying numerous aircraft icons over a region. It also includes promotional text for a global flight tracking data feed.
Once you create an API key, you can monitor usage and quotas from the AeroAPI dashboard.
The image shows a FlightAware AeroAPI usage dashboard, displaying a line graph of flight call API usage over time, with a summary of total calls and cost below.
Set your FlightAware API key in the environment before running the code. For example, on macOS/Linux:
Overview of the solution
  • Implement a get_flight_status LangChain tool that:
    • Calls the AeroAPI for flights on the current day.
    • Picks the best available timestamps using the priority estimated > actual > scheduled.
    • Converts UTC timestamps to the local timezone of origin/destination.
    • Returns a human-readable status string.
  • Register the tool along with the Python REPL tool.
  • Create a ReAct-style agent prompt so the agent plans (Thought), calls tools (Action), observes results, and provides a final answer.
  • Use the Python REPL tool for follow-up computations (e.g., add hours to arrival time).
Tool implementation
  • Save the following implementation as flight_agent.py. This function uses requests to call the AeroAPI, chooses the best time fields, converts times from UTC to local timezones via pytz, and returns a readable status string.
Quick testing of the tool (direct calls)
  • With AEROAPI_KEY set and network available, you can call the tool directly:
Register tools and create the ReAct agent
  • Register the two tools (custom flight tool + Python REPL) and construct a ReAct prompt that directs the agent to think, act, observe, and repeat until it produces the final answer.
Invoke the agent
  • Send a flight query to the agent executor. The ReAct loop will call get_flight_status, observe the API result, optionally perform follow-up calculations via the Python REPL, and then produce a final answer.
Typical verbose execution (illustrative)
  • When agent_executor runs in verbose mode it logs the Thought/Action/Observation steps. Example:
Using the Python REPL tool for datetime math
  • The Python REPL tool is useful for follow-up computations, such as determining the time to book a cab after arrival.
Example: add 3 hours to the arrival time:
For 4.5 hours:
Tool summary Best practices and production considerations
  • Always set your AEROAPI_KEY environment variable before running the agent.
  • The timestamp selection prioritizes estimated over actual over scheduled.
  • Handle HTTP/network errors and JSON parsing gracefully in production (the example raises errors for clarity).
  • Be mindful of API quotas and rate limits—use caching or debounce frequent queries when appropriate.
  • Extend the agent with additional tools (weather, maps, booking APIs) to support richer interactions.
FlightAware’s free tier may have limitations on calls and data. Monitor usage in the AeroAPI dashboard and upgrade if you need higher quotas or commercial support.
References and further reading This concludes the lesson on building a real-time flight agent using a custom LangChain tool and the Python REPL tool. Experiment by adding more tools to extend capabilities and support richer, multi-step queries.

Watch Video