Skip to content
AUTH

A 300-Line LangGraph Alternative


A 300-Line LangGraph Alternative

Modern agent frameworks such as LangGraph allow developers to build complex workflows using graph-based execution models.

Example architecture:

User Input
Reasoning Node
Tool Node
Observation Node
Next Reasoning Step

Each node represents a stage in the agent workflow.

However, the core mechanics behind these frameworks are surprisingly simple.

By combining the components we built earlier, we can construct a minimal agent runtime in roughly a few hundred lines of code.


The Core Idea

At its heart, an agent framework only needs a few elements:

ComponentPurpose
statetrack execution context
nodeunit of computation
graphworkflow structure
executorruns the graph
toolsinteract with external systems

Conceptually:

State
Node Execution
State Update
Next Node

This loop continues until the workflow finishes.


Defining the Agent State

The agent state stores all information required during execution.

Example fields include:

Example structure:

class AgentState:
def __init__(self, goal):
self.goal = goal
self.history = []
self.observations = []
self.step = 0

This structure persists information across reasoning steps.


Defining Nodes

A node represents a stage in the workflow.

Examples include:

Node TypePurpose
reasoning nodegenerate next action
tool nodeexecute external tool
observation nodeupdate state

Conceptually:

Node(Input State) → Output State

Example Reasoning Node

The reasoning node calls the language model.

def reasoning_node(state, llm):
prompt = f"Goal: {state.goal}\nHistory: {state.history}"
thought = llm.generate(prompt)
state.history.append(thought)
return thought

This node produces the next reasoning step.


Tool Execution Node

The tool node dispatches tool calls generated by the model.

def tool_node(action, tools):
tool = tools[action["name"]]
result = tool(**action["args"])
return result

The result becomes a new observation.


Updating the State

Observations are added to the agent state.

Example:

Observation → added to state.observations
state.observations.append(result)

This information influences the next reasoning step.


Graph-Based Execution

Agent workflows can be represented as a graph of nodes.

Example graph:

Reason → Tool → Observe → Reason

Graph representation:

Nodes
├─ Reason
├─ Tool
└─ Observe

Transitions define how execution moves between nodes.


Minimal Graph Executor

The executor controls the workflow.

Conceptually:

Initialize state
Execute node
Update state
Select next node
Repeat

Example Executor Implementation

def run_agent(goal, tools, llm):
state = AgentState(goal)
while True:
thought = reasoning_node(state, llm)
action = parse_action(thought)
if action["name"] == "finish":
return action["args"]["answer"]
result = tool_node(action, tools)
state.observations.append(result)
state.step += 1

This loop forms a fully functional minimal agent runtime.


Adding Time-Travel Debugging

The runtime can also record snapshots.

Example:

Step 1 → reasoning
Step 2 → tool call
Step 3 → observation

These snapshots enable replay and debugging.


Runtime Architecture Overview

After combining all components, the final runtime looks like this:

Minimal Agent Framework
├─ Agent State
├─ Reasoning Node
├─ Tool Node
├─ Graph Executor
├─ Tool Registry
└─ Execution History

This architecture closely mirrors modern agent frameworks.


Comparing with LangGraph

LangGraph provides additional features such as:

However, the core execution logic remains similar.

FeatureMinimal RuntimeLangGraph
agent loop
tool calling
graph executionbasicadvanced
persistence
distributed execution

Understanding the minimal version makes it easier to understand advanced frameworks.


What We Built in Module 11

In this module we constructed the core components of an agent runtime.

ArticleConcept
11.1why build an agent runtime
11.2agent state machine
11.3tool calling system
11.4time-travel debugging
11.5minimal agent framework

These components form the foundation of modern agent systems.


Looking Ahead

In the final module of this series we will build complete agent applications.

These capstone projects will combine everything covered so far, including:

→ Continue to 12.1 — The Computer-Use Researcher