Skip to main content
Alpha Notice: These docs cover the v1-alpha release. Content is incomplete and subject to change.For the latest stable version, see the v0 LangChain Python or LangChain JavaScript docs.
Welcome to LangChain! This quickstart will take you from zero to a fully functional AI agent in just a few minutes. We’ll start simple and gradually build up to something more sophisticated.

Super quick start

Let’s begin with the absolute basics - creating a simple agent that can answer questions and use tools:
from langchain.agents import create_agent

def get_weather(city: str) -> str:
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"

agent = create_agent(
    model="anthropic:claude-3-7-sonnet-latest",
    tools=[get_weather],
    prompt="You are a helpful assistant",
)

# Run the agent
agent.invoke(
    {"messages": [{"role": "user", "content": "what is the weather in sf"}]}
)
What just happened? We created an agent with:
  • A language model (Claude 3.7 Sonnet)
  • A simple tool (weather function)
  • A basic prompt
  • The ability to invoke it with messages

Building a real-world agent

Now let’s create something more practical. We’ll build a weather forecasting agent that demonstrates the key concepts you’ll use in production:
  1. Detailed system prompts for better agent behavior
  2. Real-world tools that integrate with external data
  3. Model configuration for consistent responses
  4. Structured output for predictable results
  5. Conversational memory for chat-like interactions
Let’s walk through each step:
1

Define the system prompt

The system prompt is your agent’s personality and instructions. Make it specific and actionable:
system_prompt = """You are an expert weather forecaster, who speaks in puns.

You have access to two tools:

- get_weather_for_location: use this to get the weather for a specific location
- get_user_location: use this to get the user's location

If a user asks you for the weather, make sure you know the location. If you can tell from the question that they mean whereever they are, use the get_user_location tool to find their location."""
2

Create tools

Tools are functions your agent can call. They should be well-documented. Oftentimes, tools will want to connect to external systems, and will rely on runtime configuration to do so. Notice here how the get_user_location tool does exactly that:
from langchain_core.tools import tool

def get_weather_for_location(city: str) -> str:  # (1)!
    """Get weather for a given city."""
    return f"It's always sunny in {city}!"

from langchain_core.runnables import RunnableConfig

USER_LOCATION = {
    "1": "Florida",
    "2": "SF"
}

@tool
def get_user_location(config: RunnableConfig) -> str:
    """Retrieve user information based on user ID."""
    user_id = config["context"].get("user_id")
    return USER_LOCATION[user_id]
3

Configure your model

Set up your language model with the right parameters for your use case:
from langchain.chat_models import init_chat_model

model = init_chat_model(
    "anthropic:claude-3-7-sonnet-latest",
    temperature=0
)
4

Define response format

Structured outputs ensure your agent returns data in a predictable format. Here, we use Python’s DataClass dictionary.
from dataclasses import dataclass

@dataclass
class WeatherResponse:
    conditions: str
    punny_response: str
5

Add memory

Enable your agent to remember conversation history:
from langgraph.checkpoint.memory import InMemorySaver

checkpointer = InMemorySaver()
6

Bring it all together

Now assemble your agent with all the components:
agent = create_agent(
    model=model,
    prompt=system_prompt,
    tools=[get_user_location, get_weather_for_location],
    response_format=WeatherResponse,
    checkpointer=checkpointer
)

config = {"configurable": {"thread_id": "1"}}
context = {"user_id": "1"}
response = agent.invoke(
    {"messages": [{"role": "user", "content": "what is the weather outside?"}]},
    config=config,
    context=context
)

response['structured_response']

response = agent.invoke(
    {"messages": [{"role": "user", "content": "thank you!"}]},
    config=config,
    context=context
)

response['structured_response']

What you’ve built

Congratulations! You now have a sophisticated AI agent that can:
  • Understand context and remember conversations
  • Use multiple tools intelligently
  • Provide structured responses in a consistent format
  • Handle user-specific information through context
  • Maintain conversation state across interactions
I