Index of every page on this site: llms.txt
All skills
kaushik-holla avatar

/langgraph-patterns

@cb7c214
Run it yourself

Canonical LangGraph 1.x patterns for typed State and reducers, conditional routers, bounded loops, interrupt/resume with Command, checkpointers (InMemory, SQLite, Postgres), runtime context for dependency injection, structured outputs, tool and MCP wiring, create_agent with middleware, subgraphs, Send fan-out, v2 streaming, retries, and LangSmith tracing, plus a common-mistakes catalog and testing patterns. Use when designing, implementing, debugging, or upgrading any LangGraph node, router, or graph, or when migrating off deprecated APIs such as create_react_agent. Reference; not a runbook.

  • 4 files
  • 34.9 KB
  • MIT
  • Updated 4 days ago
  • GitHub
Use this Skill: https://skilld.dev/gh/kaushik-holla/agent-skills/langgraph-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesadvanced-patterns.md

≈614 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Advanced LangGraph Patterns

Loaded on demand from the langgraph-patterns skill. Imports and the State, CONTEXT, and MOCK_DATA names follow the main SKILL.md.

Subgraphs

Extract a self-contained sub-pipeline into a subgraph when:

  • It has its own bounded loop (e.g., a multi-attempt scraper).
  • It's reusable across graphs.
  • The parent state would otherwise become unwieldy.
class SubState(TypedDict):
    query: str
    attempts: int
    result: dict[str, Any]


def build_research_subgraph():
    g = StateGraph(SubState)
    g.add_node("search", lambda s: {"result": MOCK_DATA.get(s["query"], {})})
    g.add_edge(START, "search")
    g.add_edge("search", END)
    return g.compile()


RESEARCH_SUB = build_research_subgraph()


def research_node(state: State) -> dict[str, Any]:
    sub = RESEARCH_SUB.invoke({"query": state["messages"][-1].content, "attempts": 0, "result": {}})
    return {"research_findings": sub["result"]}

If parent and subgraph share state keys, add the compiled subgraph directly as a node (g.add_node("research", RESEARCH_SUB)) and LangGraph maps the shared keys for you.

Async and Streaming

Stream tokens with stream_mode="messages" and the unified v2 StreamPart format:

async def run_streaming(graph, user_text: str, thread_id: str):
    config = {"configurable": {"thread_id": thread_id}}
    async for part in graph.astream(
        {"messages": [HumanMessage(content=user_text)]},
        config,
        context=CONTEXT,
        stream_mode="messages",
        version="v2",
    ):
        if part["type"] == "messages":
            chunk, meta = part["data"]  # meta["langgraph_node"] names the node
            if chunk.content:
                yield chunk.content

Notes:

  • Every v2 part is a dict with type, ns, and data. Combine modes with stream_mode=["updates", "messages", "custom"] and branch on part["type"].
  • "updates" yields per-node state deltas (good for progress UIs); "values" yields full state after each step; "custom" carries anything a node writes via get_stream_writer().
  • astream_events still exists for fine-grained callback events, but stream_mode="messages" is the simpler, recommended path for tokens.
  • Mixing async nodes with sync nodes is supported but adds friction; prefer all-async for clarity.
  • Cancellation: cancelling the consuming task cleanly leaves the checkpointer at the last completed step.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No third-party reports yet.

Signed by skilld at cb7c214. 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 4 days ago
metadata
{
  "author": "kaushik-holla",
  "version": "2.0",
  "langgraph": ">=1.2,<2"
}

README badge

README badge for kaushik-holla/agent-skills/langgraph-patterns