LangChain 1 cheatsheet

LangChain 1: Agents and tools

6 common LangChain 1 tasks.

Define a tool

Turn a function into a tool. The docstring tells the model when to use it.

from langchain.tools import tool

@tool
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    return f"It is sunny in {city}."

Create an agent

Create an agent that calls tools in a loop until it can answer. This replaces the older create_react_agent.

from langchain.agents import create_agent

agent = create_agent(
    model="anthropic:claude-sonnet-5-5",
    tools=[get_weather],
    system_prompt="You are a helpful assistant.",
)

Run an agent

Send a conversation to the agent. The result holds the full list of messages.

result = agent.invoke({"messages": [{"role": "user", "content": "What is the weather in Paris?"}]})
print(result["messages"][-1].content)

Stream agent steps

Print each step as the agent calls tools and answers.

for step in agent.stream(
    {"messages": [{"role": "user", "content": "Weather in Paris?"}]},
    stream_mode="updates",
):
    print(step)

Remember the conversation

Add a checkpointer and a thread ID so the agent keeps context between calls.

from langgraph.checkpoint.memory import InMemorySaver

agent = create_agent(model="anthropic:claude-sonnet-5-5", tools=[get_weather], checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "user-1"}}
agent.invoke({"messages": [{"role": "user", "content": "Hi, I'm Sam."}]}, config)

Return structured output from an agent

Pass a Pydantic class as response_format and read the parsed result.

agent = create_agent(model="anthropic:claude-sonnet-5-5", tools=[], response_format=Movie)
result = agent.invoke({"messages": [{"role": "user", "content": "Tell me about Inception."}]})
result["structured_response"]

Was this page helpful?