Hands-on Agentic AI: LangChain 1.0

LangChain 1.0: A visual explanation of how Agentic AI combines a model (the brain) with tools (the wrench and search) to achieve complex tasks.

What is Agentic AI? 

A simple way to think about Agentic AI is: model + tools = agentic AI.  

With LangChain 1.0 now stable, building them is straightforward. 

Let's build a 'Weather Poet' agent that:

  1. Uses a web search tool to find a forecast.
  2. Write a poem about it.

Quick Setup 

Get Your Google AI API Key for free (LLM):

  1. Go to https://aistudio.google.com/api-keys.
  2. Click "Create API key". Create a new project (e.g., "weather-poet-project") if prompted.
  3. Copy your newly generated API key.


Get Your Tavily API Key for free (Search Tool):

  1. Go to https://www.tavily.com/ and sign up.
  2. After verifying your email, copy the API key from your dashboard.


Set Up Your Python Environment:

  1. Use Python 3.10 or later.
  2. Install Libraries:  pip install langchain langchain-tavily langchain-google-genai

The Simple Version: Get the Answer

This code creates the agent and prints its final creative response.

import os
from langchain.agents import create_agent
from langchain_tavily import TavilySearch

# IMPORTANT: Replace with your own keys.
os.environ["GOOGLE_API_KEY"] = "YOUR_GOOGLE_API_KEY_HERE"
os.environ["TAVILY_API_KEY"] = "YOUR_TAVILY_API_KEY_HERE"
TARGET = "Tel Aviv"

# 1. Create the agent with a model and a tool
agent = create_agent(
model="google_genai:gemini-2.5-flash",
tools=[TavilySearch(max_results=5)]
)

# 2. Define the task
message = f"Search for tomorrow's weather in {TARGET} and write a short poem about it."

# 3. Run the agent
output = agent.invoke({
"messages": [{"role": "user", "content": message}]
})

# 4. Print the final response
response = output["messages"][-1].content[0]["text"]
print(response)


Your output would be a creative poem about tomorrow's  weather:

In Tel Aviv, the sun will shine so bright,
A gentle breeze, a lovely, clear blue light.
With temperatures around twenty-six degrees,
Tomorrow's forecast brings comfort and ease.

The Transparent Version: Cite Your Sources

For transparency, you can also see the sources the agent used. 

This version extracts the exact URLs from the agent's process.

import json
import os
from langchain.agents import create_agent
from langchain_core.messages import AIMessage, ToolMessage
from langchain_tavily import TavilySearch

# --- Setup is the same as above ---
os.environ["GOOGLE_API_KEY"] = "YOUR_GOOGLE_API_KEY_HERE"
os.environ["TAVILY_API_KEY"] = "YOUR_TAVILY_API_KEY_HERE"
TARGET = "Tel Aviv"

agent = create_agent(
model="google_genai:gemini-2.5-flash",
tools=[TavilySearch(max_results=5)]
)

message = f"Search for tomorrow's weather in {TARGET} and write a short poem about it."

output = agent.invoke({
"messages": [{"role": "user", "content": message}]
})

# Extract the final text response
output_messages = output["messages"]

response_text = next(
message.content[0]["text"]
for message in reversed(output_messages)
if isinstance(message, AIMessage) and not message.tool_calls
)

print(response_text)

# Extract the URLs the agent used
response_urls = [
result["url"]
for message in output_messages
if isinstance(message, ToolMessage)
for result in json.loads(message.content).get("results")
if "url" in result
]

print(response_urls)

Your output would be a creative poem about tomorrow's  weather with sources:

In Tel Aviv, a sunny sky,
With temperatures around twenty-six,
A gentle autumn day goes by,
No need for heavy coats or tricks.
The breeze will blow, a mild delight,
As day unfolds in golden light.
Sources: ['https://www.weatherapi.com/', 'https://www.easeweather.com/asia/isra
el/tel-aviv/october', 'https://wisemeteo.com/en/country/israel/region/tel-aviv-
district/city/tel-aviv-yafo/date/10-26', 'https://www.weather25.com/asia/israel
/tel-aviv?page=month&month=October', 'https://predictwind.com/weather/israel/te
l-aviv-district/tel-aviv/october?nxtPcountry=israel&nxtPstate=tel-aviv-district
&nxtPcity=tel-aviv&nxtPmonth=october']

Conclusion

LangChain 1.0 provides a stable toolkit for building agents that can reason and use tools. 

The ability to start simple and then add features like source extraction makes it a powerful framework for practical AI applications. 

Popular posts from this blog

Hands-on Agentic AI App: LangGraph 1.0

Hands-on Generative AI