All skills
langchain-ai avatar

/langgraph-fundamentals

@657ca08 official

INVOKE THIS SKILL when writing ANY LangGraph code. Covers StateGraph, state schemas, nodes, edges, Command, Send, invoke, streaming, and error handling.

Use this Skill: https://skilld.dev/gh/langchain-ai/langchain-skills/langgraph-fundamentals

This session only. Nothing lands on disk.

SKILL.md

≈44 tokens always: the name and description. ≈1.4k when used: this file. ≈4.6k more on demand in 2 files.

<overview> LangGraph models agent workflows as **directed graphs**:
  • StateGraph: Main class for building stateful graphs
  • Nodes: Functions that perform work and update state
  • Edges: Define execution order (static or conditional)
  • START/END: Special nodes marking entry and exit points
  • State with Reducers: Control how state updates are merged

Graphs must be compile()d before execution. </overview>

<design-methodology>

Designing a LangGraph application

Follow these 5 steps when building a new graph:

  1. Map out discrete steps — sketch a flowchart of your workflow. Each step becomes a node.
  2. Identify what each step does — categorize nodes: LLM step, data step, action step, or user input step. For each, determine static context (prompt), dynamic context (from state), retry strategy, and desired outcome.
  3. Design your state — state is shared memory for all nodes. Store raw data, format prompts on-demand inside nodes.
  4. Build your nodes — implement each step as a function that takes state and returns partial updates.
  5. Wire it together — connect nodes with edges, add conditional routing, compile with a checkpointer if needed.
</design-methodology><when-to-use-langgraph>
Use LangGraph When Use Alternatives When
Need fine-grained control over agent orchestration Quick prototyping → LangChain agents
Building complex workflows with branching/loops Simple stateless workflows → LangChain direct
Require human-in-the-loop, persistence Batteries-included features → Deep Agents
</when-to-use-langgraph>

State Management

<state-update-strategies>
Need Solution Example
Overwrite value No reducer (default) Simple fields like counters
Append to list Reducer (operator.add / concat) Message history, logs
Custom logic Custom reducer function Complex merging
</state-update-strategies>

Nodes

<node-function-signatures>

Node functions return partial state updates. Signatures for configuration and runtime access differ by language; use the applicable implementation reference.

</node-function-signatures>

Edges

<edge-type-selection>
Need Edge Type When to Use
Always go to same node add_edge() Fixed, deterministic flow
Route based on state add_conditional_edges() Dynamic branching
Update state AND route Command Combine logic in single node
Fan-out to multiple nodes Send Parallel processing with dynamic inputs
</edge-type-selection>

Command

Command combines state updates and routing in a single return value. Fields:

  • update: State updates to apply (like returning a dict from a node)
  • goto: Node name(s) to navigate to next
  • resume: Value to resume after interrupt() — see human-in-the-loop skill
<command-return-type-annotations>

Python: Use Command[Literal["node_a", "node_b"]] as the return type annotation to declare valid goto destinations.

TypeScript: Pass { ends: ["node_a", "node_b"] } as the third argument to addNode to declare valid goto destinations.

</command-return-type-annotations><warning-command-static-edges>

Warning: Command only adds dynamic edges — static edges defined with add_edge / addEdge still execute. If node_a returns Command(goto="node_c") and you also have graph.add_edge("node_a", "node_b"), both node_b and node_c will run.

</warning-command-static-edges>

Send API

Fan-out with Send: return [Send("worker", {...})] from a conditional edge to spawn parallel workers. Requires a reducer on the results field.


Running Graphs: Invoke and Stream

<invoke-basics>

Call graph.invoke(input, config) to run a graph to completion and return the final state.

</invoke-basics><stream-mode-selection>
Mode What it Streams Use Case
values Full state after each step Monitor complete state
updates State deltas Track incremental updates
messages LLM tokens + metadata Chat UIs
custom User-defined data Progress indicators
</stream-mode-selection>

Error Handling

Match the error type to the right handler:

<error-handling-table>
Error Type Who Fixes Strategy Example
Transient (network, rate limits) System RetryPolicy(max_attempts=3) add_node(..., retry_policy=...)
LLM-recoverable (tool failures) LLM ToolNode(tools, handle_tool_errors=True) Error returned as ToolMessage
User-fixable (missing info) Human interrupt({"message": ...}) Collect missing data (see HITL skill)
Unexpected Developer Let bubble up raise
</error-handling-table>

Core boundaries

  • Return partial state updates from nodes instead of mutating state directly.
  • Route loops through a named node; START is entry-only.
  • Define reducers for accumulated list fields; otherwise, the last write wins.
  • Account for static edges when using Command with goto, because both routes execute.

Implementation references

If writing, modifying, or debugging LangGraph code, determine the project's language from its existing files, then read the applicable reference before implementing:

Read both only when the task covers both languages. For conceptual questions that require no code, do not load either reference.

Source: SKILL.md on GitHub

No alerts14d5 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    This skill provides educational documentation and code examples for building agent workflows with LangGraph. No security issues were detected during analysis.

  • Socket14d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

  • Runlayer7mo

    1 file scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 657ca08. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated 2 weeks ago
  • Python
  • TypeScript
  • langgraph
  • state-management
  • workflow-orchestration
  • agent-patterns
  • graph-based-systems
  • conditional-routing

README badge

README badge for langchain-ai/langchain-skills/langgraph-fundamentals

Builds directed graph workflows using StateGraph, nodes, edges, and state schemas with reducers. Covers state design, node function signatures, conditional routing with Command, and compilation — essential for any LangGraph application in Python or TypeScript.

Generated from the current SKILL.md.

What is a reducer and when do I need one?
A reducer controls how state updates are merged. Use one when a field should accumulate values (like appending to a list or summing integers) rather than overwriting. Without a reducer, returning a list from a node will overwrite the previous value instead of appending to it.
What should a node function return?
Node functions must return a partial update (a dict with only the fields being changed), not the full state object. Returning the entire state object or mutating state in place will cause errors.
When should I use Command instead of add_edge or add_conditional_edges?
Use Command when you need to update state and decide the next node to route to in a single return value. This is cleaner than using separate conditional edges when the routing decision depends on computed state changes.
What arguments can a node function accept?
A node can accept just state, or state plus config (to access thread_id or configurable values), or state plus runtime (for Python only, to access context and store). The signature determines what information is available to the node.
Do I need to compile a graph before running it?
Yes. Graphs must be compiled with .compile() before invoking or streaming them. Compilation prepares the graph for execution.

Generated from the current SKILL.md. These answers refresh after source changes.