February 1, 2025
(APIs, Browsers, File Systems)
Equipping your AI agents with tool access is what unlocks true autonomy. A goal-driven agent isnβt truly intelligent until it can interact with APIs, explore websites, or handle files. In this tutorial, weβll show you how to connect your agents to the outside world using LangChain and CrewAIβand do it safely and effectively.
Large Language Models (LLMs) are great at reasoning and generating text, but they are limited to their training data and donβt have native access to real-time information, APIs, or environments.
Tools bridge that gap. They allow agents to:
Frameworks like LangChain and CrewAI include built-in support for tools, enabling agents to decide when and how to use them autonomously.
| Tool Type | Example Use Case | Libraries & Tools |
|---|---|---|
| REST APIs | Weather, stock prices, search, CRM | requests, LangChain tools, Zapier API |
| Browser Access | Scraping or interacting with web UIs | Playwright, Puppeteer, Selenium |
| File System | Reading/writing files, logs, config updates | Python file I/O, OS commands |
| Search Engines | Context lookup, product info, news | Tavily API, Serper API, Google Search API |
LangChain provides a convenient way to wrap Python functions into callable tools using the Tool class.
pythonCopyEditfrom langchain.tools import Tool
import requests
def get_weather(city):
response = requests.get(f"https://api.weatherapi.com/v1/current.json?q={city}&key=YOUR_KEY")
return response.json()["current"]["condition"]["text"]
weather_tool = Tool(
name="GetWeather",
func=get_weather,
description="Use this tool to get current weather for a given city"
)pythonCopyEditfrom langchain.agents import initialize_agent
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0)
tools = [weather_tool]
agent = initialize_agent(tools, llm, agent_type="zero-shot-react-description")
response = agent.run("What's the weather like in San Francisco?")
print(response)
βοΈ This agent will now decide on its own to call the weather API tool when needed.
CrewAI takes tool access a step further by associating specific tools with specific agentsβwhich is great for multi-agent teamwork.
pythonCopyEditfrom crewai import Agent, Task, Crew
from langchain.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
researcher = Agent(
role="Researcher",
goal="Find recent articles on AI governance",
backstory="You're a brilliant analyst at a tech think tank.",
tools=[search]
)
task = Task(description="Summarize the top 3 findings", agent=researcher)
crew = Crew(agents=[researcher], tasks=[task])
crew.kickoff()π Agents in CrewAI can each have their own toolset, making it easy to assign roles like:
π View CrewAI Docs
pythonCopyEditfrom langchain.tools import Tool
import subprocess
def search_google(query):
subprocess.run(["playwright", "open", f"https://google.com/search?q={query}"])
return "Opened browser tab."
google_tool = Tool(name="BrowserSearch", func=search_google, description="Launches a browser search.")β οΈ Safety Tip: Always sandbox browser tools or run in isolated environments.
You can allow agents to read or write files, but always wrap actions with controlled access logic.
pythonCopyEditimport os
def read_file(file_path):
if os.path.exists(file_path):
with open(file_path, "r") as f:
return f.read()
return "File not found."
file_tool = Tool(name="ReadFile", func=read_file, description="Read contents of a file.")π Use cases: Code analysis agents, config updaters, file transformers.
π LangChain Agent Tool Usage Example
π LangChain Tool Docs
π CrewAI Tool Usage