Skip to main content
Now that we understand the concept of tools in LangChain, this lesson puts everything together by building a compact custom tool that returns flight status information. The example demonstrates:
  • How to convert a Python function into a LangChain tool using the @tool decorator.
  • How to inspect generated tool metadata (name, description, args).
  • How to call the tool, use its output as context in a prompt, and run a prompt → LLM → output-parser chain.
The following concise, corrected example shows the end-to-end flow.
Expected metadata printed when inspecting the tool:
Example of the chain invocation outputs (based on the static tool response above):
Tool metadata reference Notes on what’s happening
  • Decorating the function with @tool converts it into a StructuredTool-like object that LangChain can inspect and call. The decorator exposes metadata such as name, description, and args (the argument schema).
  • GetFlightStatus.run(flight) executes the function and returns the static context string shown above. In production you would call a live flight-status API inside this function and return the real response.
  • The PromptTemplate uses the tool output as context. The chain (prompt | llm | output_parser) takes the populated prompt, sends it to the LLM, and then parses the output into a simple string using StrOutputParser.
  • This follows a retrieve-and-read pattern where the retrieval step is replaced by a tool call that supplies up-to-date context to the LLM.
This example uses a static response to keep the demonstration simple. For production, replace the static return with a real API call (include robust error handling, retries, and rate limiting). Also ensure the tool returns well-structured, documented data that your prompt and output parser expect.
Next steps
  • Replace the static GetFlightStatus implementation with a real flight-status API to return live information.
  • Build additional tools (e.g., airport info, weather) and explore creating an agent that selects between them to fulfill more complex user requests.
  • Read more about LangChain tools and agents:
By following this pattern you can develop robust tool-backed chains that keep the LLM focused on reasoning while delegating data retrieval and structured logic to external functions.

Watch Video

Practice Lab