Quick Start

This chapter builds your first tool-using agent with LangChain 1.0's create_agent.


Minimal agent

from langchain.agents import create_agent

def get_weather(city: str) -> str:
    """Return fake weather for demo."""
    return f"It's sunny in {city}."

agent = create_agent(
    "openai:gpt-4.1-mini",
    tools=[get_weather],
    system_prompt="You are a helpful assistant. Use tools when needed.",
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "What's the weather in Shanghai?"}]
})

print(result["messages"][-1].content)

What happens:

  1. The model reads the user message
  2. It chooses to call get_weather
  3. The framework runs the tool and feeds the result back
  4. The model writes the final answer

@tool decorator

Add types and docstrings so the model knows how to call tools:

from langchain.tools import tool
from langchain.agents import create_agent

@tool
def search(query: str) -> str:
    """Search the web for information."""
    return f"Results for: {query}"

agent = create_agent("openai:gpt-4.1-mini", tools=[search])

Streaming

for chunk in agent.stream(
    {"messages": [{"role": "user", "content": "Explain RAG in one paragraph."}]},
    stream_mode="messages",
):
    if chunk.content:
        print(chunk.content, end="", flush=True)

Use stream_mode="updates" to see per-node updates (including tool calls).


Structured output

from pydantic import BaseModel
from langchain.agents import create_agent

class Answer(BaseModel):
    summary: str
    confidence: float

agent = create_agent(
    "openai:gpt-4.1-mini",
    tools=[],
    response_format=Answer,
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "Summarize LangChain 1.0 in one sentence."}]
})
print(result["structured_response"])

Multi-turn memory (preview)

Use a checkpointer and thread_id (Memory & Checkpoints):

from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(
    "openai:gpt-4.1-mini",
    tools=[],
    checkpointer=InMemorySaver(),
)

config = {"configurable": {"thread_id": "demo-thread-1"}}

agent.invoke(
    {"messages": [{"role": "user", "content": "My name is Alex."}]},
    config=config,
)
result = agent.invoke(
    {"messages": [{"role": "user", "content": "What's my name?"}]},
    config=config,
)
print(result["messages"][-1].content)

Checklist

  1. venv + pip install langchain langgraph langchain-openai
  2. Set OPENAI_API_KEY
  3. Run the minimal agent
  4. Add a @tool and watch tool calls
  5. Try stream and checkpointer

Next steps