Skip to content
Starfield Labs

Articles

How to build an AI agent: a beginner's guide with LangChain, LangGraph and CrewAI

Tools, a loop and a stop. Your first agent in about 20 lines of Python.

By Starfield LabsPublished

Cover art with a network of connected agents beside the headline How to build an AI agent.

An AI agent is a model that can use tools in a loop: it decides what to do, calls a tool, looks at the result and repeats until the job is done. The quickest way to build one in Python is LangChain's create_agent, which gives you that loop in about 20 lines. When you need more control, LangGraph lets you design the loop yourself, and CrewAI lets several agents with different roles work together.

What makes something an agent?

A chatbot answers. An agent acts. Three things make the difference:

  1. Tools. It can do something: search, run code, read a file, call an API.
  2. A loop. It looks at each result and picks the next step.
  3. A stop. It knows when it's done, or when to ask you.

Start with one tool and a clear task. Most broken agent demos have too many tools and no limit on how many steps they take.

Which framework should you use?

Framework Best for How it works
LangChain create_agent Your first agent; one assistant with a few tools Gives you a ready-made tool loop
LangGraph Agents with custom steps, branches, memory or human approval You draw the loop as a graph of steps
CrewAI Several agents with different roles working on one job You define agents, tasks and a crew

All three are Python libraries and all need an API key for a model provider. For free options, see free LLM APIs for students and hackathons.

Step 1: Build a simple agent with LangChain

Install LangChain and set your model key:

pip install -U langchain langchain-anthropic
export ANTHROPIC_API_KEY="your-key"

Then create agent.py:

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


@tool
def word_count(text: str) -> int:
    """Count the words in a piece of text."""
    return len(text.split())


agent = create_agent(
    model="anthropic:claude-sonnet-4-6",
    tools=[word_count],
    system_prompt="You help people edit their writing. Use tools when they help.",
)

result = agent.invoke(
    {"messages": [{"role": "user", "content": "How many words are in: 'Build first, study second'?"}]}
)
print(result["messages"][-1].content)

What happens: the model reads the question, decides to call word_count, reads the answer (4) and writes a reply. The @tool decorator turns a Python function into a tool, and the docstring tells the model when to use it. Swap the model string to use another provider, in the form provider:model.

Step 2: Take control of the loop with LangGraph

create_agent hides the loop. LangGraph shows it. You describe your agent as a graph:

  • State: the data passed between steps, usually the list of messages.
  • Nodes: steps, written as Python functions (call the model, run the tools).
  • Edges: which step comes next, including conditional edges ("if the model asked for a tool, run it; otherwise stop").

The core of a tool-calling agent in LangGraph looks like this:

from langgraph.graph import StateGraph, START, END

builder = StateGraph(MessagesState)          # state: the message list
builder.add_node("llm_call", llm_call)       # step 1: ask the model
builder.add_node("tool_node", tool_node)     # step 2: run the tools it asked for
builder.add_edge(START, "llm_call")
builder.add_conditional_edges("llm_call", should_continue, ["tool_node", END])
builder.add_edge("tool_node", "llm_call")    # loop back to the model
agent = builder.compile()

The LangGraph quickstart builds the full version of this, a calculator agent, step by step. Use LangGraph when you need things create_agent can't do simply: a fixed approval step before an action, branches for different request types, or memory across sessions.

Step 3: Make agents work as a team with CrewAI

CrewAI gives each agent a role, a goal and a backstory, then hands them tasks in order. Install it with uv:

uv tool install crewai

A two-agent crew that researches and then writes:

from crewai import Agent, Crew, Process, Task

researcher = Agent(
    role="Researcher",
    goal="Find the key facts about {topic}",
    backstory="A careful analyst who checks every claim.",
)
writer = Agent(
    role="Writer",
    goal="Explain {topic} in plain words",
    backstory="A writer who turns notes into short, clear posts.",
)

research = Task(
    description="List five important facts about {topic}.",
    expected_output="Five bullet points.",
    agent=researcher,
)
write = Task(
    description="Write a 150-word explainer using the research.",
    expected_output="A 150-word explainer.",
    agent=writer,
)

crew = Crew(agents=[researcher, writer], tasks=[research, write], process=Process.sequential)
print(crew.kickoff(inputs={"topic": "edge AI"}))

For a full project with config files, CrewAI's CLI sets everything up: crewai create crew my_project, then crewai install and crewai run.

How do you make an agent reliable?

  • Write tool descriptions like instructions. Say what the tool does, what to pass in and when to use it.
  • Limit the steps. Cap the number of loops so a confused agent stops instead of running all night.
  • Ask before acting. Anything that sends, deletes, pays or changes data should need a person's approval.
  • Check outputs in code. If you need data in a set shape, validate it and retry once if it's wrong.
  • Test with real tasks. Write 20 realistic requests, run them and read every answer.

What should you build first?

Good first agents are small and useful: a research assistant that searches and summarises, an inbox helper that drafts replies, a data agent that answers questions about a CSV. For more, see AI agent examples. To give your agent tools other apps can use too, build an MCP server.

Where to go next

Agents are one of the most popular things to build at AI hackathons. Sign up at starfieldlabs.ai to hear first about our events. For the ideas behind the code, read AI agents: types, uses and limits, or follow the tutorial build an agent that answers questions about a code repo.

Sources (opened 20 November 2026)

  • LangChain, Quickstart: create_agent, @tool, invoke, provider:model strings.
  • LangChain, LangGraph quickstart: StateGraph, nodes, edges, START/END, compile.
  • CrewAI, Crews and Installation: Agent, Task, Crew, Process.sequential, kickoff(inputs=...), uv tool install crewai.

Keep reading

All articles