Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

LangChain Agents in Practice: Tools, Memory, and create_agent (Code Tutorial)

55 sec read

Prerequisites

  • Python 3.10+
  • Install: pip install -U langchain langgraph pydantic
  • An API key for your model provider

Basic agent with a tool

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

@tool
def check_order_status(order_id: str) -> str:
    """Look up the shipping status for an order ID."""
    return f"Order {order_id} is out for delivery."

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[check_order_status],
    system_prompt="You are a helpful support assistant.",
)

Add memory across turns

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

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[check_order_status],
    checkpointer=InMemorySaver(),
)

config = {"configurable": {"thread_id": "user-42"}}
agent.invoke({"messages": [{"role": "user", "content": "Check order 4471"}]}, config=config)
agent.invoke({"messages": [{"role": "user", "content": "Is it late?"}]}, config=config)

Add retry and guardrail middleware

from langchain.agents.middleware import ModelRetryMiddleware, PIIMiddleware

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[check_order_status],
    middleware=[
        ModelRetryMiddleware(max_retries=3),
        PIIMiddleware("email"),
    ],
)

Force structured output

from pydantic import BaseModel
from langchain.agents import create_agent

class OrderAnswer(BaseModel):
    order_id: str
    status: str

agent = create_agent(model="openai:gpt-4o-mini", tools=[check_order_status], response_format=OrderAnswer)
result = agent.invoke({"messages": [{"role": "user", "content": "Status of order 4471?"}]})
result["structured_response"]

Common failure modes are rarely exotic: an ambiguous tool description, no cap on iterations, or untyped tool arguments. Precise docstrings, a max_iterations limit, and Pydantic-validated inputs fix most of them. When a single agent loop isn’t enough, that is usually the signal to move the workflow into LangGraph.

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Kueue on Kubernetes: GPU Job Queueing and Fair-Share Quotas…

The default Kubernetes scheduler has no idea your GPUs are shared between teams. This hands-on lab uses Kueue to add job level admission, per...
Ajeet Raina
7 min read
Join our Discord Server