How to Build Your First AI Agent with LangChain in 30 Minutes

A working LangChain agent in under 100 lines of Python. Calls a real tool, handles errors, deploys to Replit. No theory — just code.

Most LangChain tutorials drown you in theory. Here's the 30-minute version that ships a working agent.

What we're building

An agent that takes a company name, looks up basic info via web search, and returns a structured summary. ~80 lines of Python.

Step 1: Setup (5 minutes)

pip install langchain langchain-openai langchain-community
export OPENAI_API_KEY=sk-...
export TAVILY_API_KEY=tvly-...  # free signup at tavily.com

Step 2: The agent (20 lines)

from langchain_openai import ChatOpenAI
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0) tools = [TavilySearchResults(max_results=3)] prompt = hub.pull("hwchase17/react")

agent = create_react_agent(llm, tools, prompt) executor = AgentExecutor(agent=agent, tools=tools, verbose=True, max_iterations=4)

result = executor.invoke({"input": "Tell me about Anthropic - founded, funding, products"}) print(result["output"]) ```

That's a working agent. Save as agent.py, run python agent.py.

Step 3: Add structured output

from langchain_core.pydantic_v1 import BaseModel

class CompanyInfo(BaseModel): name: str founded: int funding_total_usd: int main_products: list[str]

structured_llm = llm.with_structured_output(CompanyInfo) # wrap the agent output through structured_llm for typed results ```

Step 4: Add error handling

try:
    result = executor.invoke({"input": query})
except Exception as e:
    # Tavily ratelimit, OpenAI timeout, etc.
    result = {"output": f"Agent failed: {e}"}

Step 5: Deploy

Push to Replit (free), set the env vars in Secrets, hit the web preview URL. Live in 5 minutes.

For production, move to Modal or Railway — both have free tiers and proper observability.

What's next

This is a ReAct agent — good for single tasks. For multi-step workflows that need state, upgrade to LangGraph (see our LangChain vs LangGraph post).

Hire an AI agent developer →

Need this built for you?

Hire a vetted Nexora expert. Escrow-protected. Fixed price. From $65.

Browse automation services →