langgraphgo

package module
v0.8.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 0 Imported by: 0

README

LangGraphGo

Abbreviated as lango, 中文: 懒狗

License go.dev reference github actions Go Report Card Coverage Status

English | 简体中文

Website: http://lango.rpcx.io

🔀 Forked from paulnegz/langgraphgo - Enhanced with streaming, visualization, observability, and production-ready features.

This fork aims for feature parity with the Python LangGraph library, adding support for parallel execution, persistence, advanced state management, pre-built agents, and human-in-the-loop workflows.

Test coverage

🌐 Websites Built with LangGraphGo

Real-world applications built with LangGraphGo:

Insight NoteX

Insight - An AI-powered knowledge management and insight generation platform that uses LangGraphGo to build intelligent analysis workflows, helping users extract key insights from massive amounts of information.

NoteX - An intelligent note-taking and knowledge organization tool that leverages AI for automatic categorization, tag extraction, and content association, making knowledge management more efficient.

📦 Installation

go get github.com/smallnest/langgraphgo

Note: This repository uses Git submodules for the showcases directory. When cloning, use one of the following methods:

# Method 1: Clone with submodules
git clone --recurse-submodules https://github.com/smallnest/langgraphgo

# Method 2: Clone first, then initialize submodules
git clone https://github.com/smallnest/langgraphgo
cd langgraphgo
git submodule update --init --recursive

🚀 Features

  • Core Runtime:

    • Parallel Execution: Concurrent node execution (fan-out) with thread-safe state merging.
    • Runtime Configuration: Propagate callbacks, tags, and metadata via RunnableConfig.
    • Generic Types: Type-safe state management with generic StateGraph implementations.
    • LangChain Compatible: Works seamlessly with langchaingo.
  • Persistence & Reliability:

    • Checkpointers: Redis, Postgres, SQLite, and File implementations for durable state.
    • File Checkpointing: Lightweight file-based checkpointing without external dependencies.
    • State Recovery: Pause and resume execution from checkpoints.
  • Advanced Capabilities:

    • State Schema: Granular state updates with custom reducers (e.g., AppendReducer).
    • Smart Messages: Intelligent message merging with ID-based upserts (AddMessages).
    • Command API: Dynamic control flow and state updates directly from nodes.
    • Ephemeral Channels: Temporary state values that clear automatically after each step.
    • Subgraphs: Compose complex agents by nesting graphs within graphs.
    • Enhanced Streaming: Real-time event streaming with multiple modes (updates, values, messages).
    • Pre-built Agents: Ready-to-use ReAct, CreateAgent, and Supervisor agent factories.
    • Programmatic Tool Calling (PTC): LLM generates code that calls tools programmatically, reducing latency and token usage by 10x.
  • Developer Experience:

    • Visualization: Export graphs to Mermaid, DOT, and ASCII with conditional edge support.
    • Human-in-the-loop (HITL): Interrupt execution, inspect state, edit history (UpdateState), and resume.
    • Observability: Built-in tracing and metrics support.
    • Tools: Integrated Tavily and Exa search tools.

🎯 Quick Start

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/smallnest/langgraphgo/graph"
	"github.com/tmc/langchaingo/llms"
	"github.com/tmc/langchaingo/llms/openai"
)

func main() {
	ctx := context.Background()
	model, _ := openai.New()

	// 1. Create Graph
	g := graph.NewMessageGraph()

	// 2. Add Nodes
	g.AddNode("generate", func(ctx context.Context, state any) (any, error) {
		messages := state.([]llms.MessageContent)
		response, _ := model.GenerateContent(ctx, messages)
		return append(messages, llms.TextParts("ai", response.Choices[0].Content)), nil
	})

	// 3. Define Edges
	g.AddEdge("generate", graph.END)
	g.SetEntryPoint("generate")

	// 4. Compile
	runnable, _ := g.Compile()

	// 5. Invoke
	initialState := []llms.MessageContent{
		llms.TextParts("human", "Hello, LangGraphGo!"),
	}
	result, _ := runnable.Invoke(ctx, initialState)
	
	fmt.Println(result)
}

📚 Examples

This project includes 85+ comprehensive examples organized into categories:

Example Categories

View All 85+ Examples →

🔧 Key Concepts

Parallel Execution

LangGraphGo automatically executes nodes in parallel when they share the same starting node. Results are merged using the graph's state merger or schema.

g.AddEdge("start", "branch_a")
g.AddEdge("start", "branch_b")
// branch_a and branch_b run concurrently
Human-in-the-loop (HITL)

Pause execution to allow for human approval or input.

config := &graph.Config{
    InterruptBefore: []string{"human_review"},
}

// Execution stops before "human_review" node
state, err := runnable.InvokeWithConfig(ctx, input, config)

// Resume execution
resumeConfig := &graph.Config{
    ResumeFrom: []string{"human_review"},
}
runnable.InvokeWithConfig(ctx, state, resumeConfig)
Pre-built Agents

Quickly create complex agents using factory functions.

// Create a ReAct agent
agent, err := prebuilt.CreateReactAgent(model, tools)

// Create an agent with options
agent, err := prebuilt.CreateAgent(model, tools, prebuilt.WithSystemMessage("System prompt"))

// Create a Supervisor agent
supervisor, err := prebuilt.CreateSupervisor(model, agents)
Programmatic Tool Calling (PTC)

Generate code that calls tools directly, reducing API round-trips and token usage.

// Create a PTC agent
agent, err := ptc.CreatePTCAgent(ptc.PTCAgentConfig{
    Model:         model,
    Tools:         toolList,
    Language:      ptc.LanguagePython, // or ptc.LanguageGo
    ExecutionMode: ptc.ModeDirect,     // Subprocess (default) or ModeServer
    MaxIterations: 10,
})

// LLM generates code that calls tools programmatically
result, err := agent.Invoke(ctx, initialState)

See the PTC README for detailed documentation.

🎨 Graph Visualization

exporter := runnable.GetGraph()
fmt.Println(exporter.DrawMermaid()) // Generates Mermaid flowchart

📈 Performance

  • Graph Operations: ~14-94μs depending on format
  • Tracing Overhead: ~4μs per execution
  • Event Processing: 1000+ events/second
  • Streaming Latency: <100ms

🧪 Testing

go test ./... -v

Contributors

This project is open for contributions! if you are interested in being a contributor please create feature issues first, then submit PRs..

📄 License

MIT License - see original repository for details.

Documentation

Overview

LangGraph Go - Building Stateful, Multi-Agent Applications in Go

LangGraph Go is a Go implementation of LangChain's LangGraph framework for building stateful, multi-agent applications with LLMs. It provides a powerful graph-based approach to constructing complex AI workflows with support for cycles, checkpoints, and human-in-the-loop interactions.

Quick Start

Install the package:

go get github.com/smallnest/langgraphgo

Basic example:

package main

import (
	"context"
	"fmt"

	"github.com/smallnest/langgraphgo/graph"
	"github.com/smallnest/langgraphgo/prebuilt"
	"github.com/tmc/langchaingo/llms/openai"
	"github.com/tmc/langchaingo/tools"
)

func main() {
	// Initialize LLM
	llm, _ := openai.New()

	// Create a simple ReAct agent
	agent, _ := prebuilt.CreateReactAgent(
		llm,
		[]tools.Tool{&tools.CalculatorTool{}},
		10, // max iterations
	)

	// Execute the agent
	ctx := context.Background()
	result, _ := agent.Invoke(ctx, map[string]any{
		"messages": []llms.MessageContent{
			{
				Role: llms.ChatMessageTypeHuman,
				Parts: []llms.ContentPart{
					llms.TextPart("What is 123 * 456?"),
				},
			},
		},
	})

	fmt.Println(result)
}

Key Features

  • Stateful Graphs: Define complex workflows with state persistence
  • Agent Orchestration: Build multi-agent systems with specialized roles
  • Checkpointing: Save and resume execution state
  • Streaming: Real-time event streaming during execution
  • Memory Management: Various strategies for conversation memory
  • Tool Integration: Extensive ecosystem of built-in tools
  • Type Safety: Generic-based typed graphs for compile-time safety
  • Visualization: Graph visualization and debugging tools

Core Concepts

Graph Structure

LangGraph Go uses a directed graph structure where:

  • Nodes represent processing units (agents, tools, functions)
  • Edges define the flow of execution
  • State flows through the graph and evolves at each node

State Management

State can be managed in different ways:

  • Untyped: Using map[string]any for flexibility
  • Typed: Using Go generics for type safety
  • Structured: Using predefined schemas

Package Structure

Core Packages

graph/ The core graph construction and execution engine

// Create a state graph
g := graph.NewStateGraph()

// Add nodes
g.AddNode("process", func(ctx context.Context, state map[string]any) (map[string]any, error) {
	state["processed"] = true
	return state, nil
})

// Define execution flow
g.SetEntry("process")
g.AddEdge("process", graph.END)

// Compile and run
runnable, _ := g.Compile()
result, _ := runnable.Invoke(ctx, initialState)

prebuilt/ Ready-to-use agent implementations

Types of agents:

  • ReAct Agent: Reason and act pattern
  • Supervisor Agent: Orchestrates multiple agents
  • Planning Agent: Creates and executes plans
  • Reflection Agent: Self-correcting agent
  • Tree of Thoughts: Multi-path reasoning

Example:

// Create a supervisor with multiple agents
members := map[string]*graph.StateRunnableUntyped
	"analyst":   analystAgent,
	"coder":     coderAgent,
	"reviewer":  reviewerAgent,
}

supervisor, _ := prebuilt.CreateSupervisor(llm, members, "router")

### memory/ Various memory management strategies

Types:

  • Buffer: Simple FIFO buffer
  • Sliding Window: Maintains recent context with overlap
  • Summarization: Compresses older conversations
  • Hierarchical: Multi-level memory with importance scoring
  • OS-like: Sophisticated paging and eviction

Example:

// Use summarization memory
memory := memory.NewSummarizationMemory(llm, 2000)
agent, _ := prebuilt.CreateChatAgent(llm, "", memory)

### tool/ Collection of useful tools

Categories:

  • Web Search: Tavily, Brave, EXA, Bocha
  • File Operations: Read, write, list files
  • Code Execution: Shell, Python
  • Web APIs: HTTP requests

Example:

// Use web search tool
searchTool, _ := tool.NewTavilySearchTool(apiKey)
agent, _ := prebuilt.CreateReactAgent(llm, []tools.Tool{searchTool}, 10)

Storage Packages

store/ Checkpoint persistence implementations

Options:

  • SQLite: Lightweight, file-based storage
  • PostgreSQL: Scalable relational database
  • Redis: High-performance in-memory storage

Example:

// PostgreSQL checkpoint store
store, _ := postgres.NewPostgresCheckpointStore(ctx, postgres.PostgresOptions{
	ConnString: "postgres://user:pass@localhost/langgraph",
})

g.WithCheckpointing(graph.CheckpointConfig{Store: store})

Adapter Packages

adapter/ Integration adapters for external systems

Adapters:

  • GoSkills: Custom Go-based skills
  • MCP: Model Context Protocol tools

Example:

// Load GoSkills
skills, _ := goskills.LoadSkillsFromDir("./skills")
tools, _ := goskills.ConvertToLangChainTools(skills)

Specialized Packages

ptc/ Programmatic Tool Calling - agents generate code to use tools

agent, _ := ptc.CreatePTCAgent(ptc.PTCAgentConfig{
	Model:    llm,
	Tools:    tools,
	Language: ptc.LanguagePython,
})

log/ Simple logging utilities

logger := log.NewDefaultLogger(log.LogLevelInfo)
listener := graph.NewLoggingListener(logger, log.LogLevelInfo, false)

RAG (Retrieval-Augmented Generation) Package

The rag package provides comprehensive RAG capabilities for LangGraph applications:

// Basic Vector RAG
llm, _ := openai.New()
embedder, _ := openai.NewEmbedder()
vectorStore, _ := pgvector.New(ctx, pgvector.WithEmbedder(embedder))

vectorRAG := rag.NewVectorRAG(llm, embedder, vectorStore, 5)
result, _ := vectorRAG.Query(ctx, "What is quantum computing?")

// GraphRAG with Knowledge Graph
graphRAG, _ := rag.NewGraphRAGEngine(rag.GraphRAGConfig{
	DatabaseURL:     "falkordb://localhost:6379",
	ModelProvider:   "openai",
	EntityTypes:     []string{"PERSON", "ORGANIZATION", "LOCATION"},
	EnableReasoning: true,
}, llm, embedder)

// Hybrid RAG combining vector and graph approaches
vectorRetriever := retrievers.NewVectorRetriever(vectorStore, embedder, config)
graphRetriever := retrievers.NewGraphRetriever(knowledgeGraph, embedder, config)
hybridRetriever := retrievers.NewHybridRetriever(
	[]rag.Retriever{vectorRetriever, graphRetriever},
	[]float64{0.6, 0.4}, config)

RAG Features

  • Multiple Retrieval Strategies: Vector similarity, graph-based, hybrid
  • Knowledge Graph Integration: Automatic entity and relationship extraction
  • Document Processing: Various loaders and text splitters
  • Flexible Storage: Support for multiple vector stores and graph databases
  • LangGraph Integration: Seamless integration with agents and workflows

Advanced Examples

1. Multi-Agent System with Supervisor

package main

import (
	"context"
	"fmt"

	"github.com/smallnest/langgraphgo/prebuilt"
	"github.com/tmc/langchaingo/llms/openai"
	"github.com/tmc/langchaingo/tools"
)

func main() {
	llm, _ := openai.New()

	// Create specialized agents
	researcher, _ := prebuilt.CreateReactAgent(llm, researchTools, 10)
	writer, _ := prebuilt.CreateReactAgent(llm, writingTools, 10)
	critic, _ := prebuilt.CreateReactAgent(llm, criticTools, 5)

	// Create supervisor
	members := map[string]*graph.StateRunnableUntyped
		"researcher": researcher,
		"writer":    writer,
		"critic":    critic,
	}

	supervisor, _ := prebuilt.CreateSupervisor(llm, members, "router")

	// Execute workflow
	result, _ := supervisor.Invoke(ctx, map[string]any{
		"messages": []llms.MessageContent{
			{
				Role: llms.ChatMessageTypeHuman,
				Parts: []llms.ContentPart{
					llms.TextPart("Write a research paper on quantum computing"),
				},
			},
		},
	})

	fmt.Println(result)
}

2. RAG (Retrieval-Augmented Generation) System

package main

import (
	"context"

	"github.com/smallnest/langgraphgo/prebuilt"
	"github.com/smallnest/langgraphgo/memory"
	"github.com/smallnest/langgraphgo/store/postgres"
	"github.com/tmc/langchaingo/embeddings/openai"
	"github.com/tmc/langchaingo/llms"
	"github.com/tmc/langchaingo/vectorstores/pgvector"
)

func main() {
	// Initialize components
	llm, _ := openai.NewChat(openai.GPT4)
	embedder, _ := openai.NewEmbedder()

	// Setup vector store
	store, _ := pgvector.New(
		ctx,
		pgvector.WithConnString("postgres://localhost/postgres"),
		pgvector.WithEmbedder(embedder),
	)

	// Create RAG agent
	rag, _ := prebuilt.CreateRAGAgent(
		llm,
		documentLoader,
		textSplitter,
		embedder,
		store,
		5, // retrieve 5 documents
	)

	// Add memory for conversation
	mem := memory.NewBufferMemory(100)
	rag.WithMemory(mem)

	// Enable checkpointing
	checkpointStore, _ := postgres.NewPostgresCheckpointStore(ctx, postgres.PostgresOptions{
		ConnString: "postgres://localhost/langgraph",
	})
	rag.WithCheckpointing(graph.CheckpointConfig{
		Store: checkpointStore,
	})

	// Query the system
	result, _ := rag.Invoke(ctx, map[string]any{
		"messages": []llms.MessageContent{
			{
				Role: llms.ChatMessageTypeHuman,
				Parts: []llms.ContentPart{
					llms.TextPart("What are the latest developments in AI?"),
				},
			},
		},
	})
}

3. Typed Graph with Custom State

package main

import (
	"context"
	"fmt"

	"github.com/smallnest/langgraphgo/graph"
	"github.com/smallnest/langgraphgo/prebuilt"
)

type WorkflowState struct {
	Input     string `json:"input"`
Processed  string `json:"processed"`
Validated  bool   `json:"validated"`
Output     string `json:"output"`
StepCount  int    `json:"step_count"`
}

func main() {
	// Create typed graph
	g := graph.NewStateGraph[WorkflowState]()

	// Add typed nodes
	g.AddNode("process", "Process the input", func(ctx context.Context, state WorkflowState) (WorkflowState, error) {
		state.Processed = strings.ToUpper(state.Input)
		state.StepCount++
		return state, nil
	})

	g.AddNode("validate", "Validate the output", func(ctx context.Context, state WorkflowState) (WorkflowState, error) {
		state.Validated = len(state.Processed) > 0
		state.StepCount++
		return state, nil
	})

	g.AddNode("output", "Generate final output", func(ctx context.Context, state WorkflowState) (WorkflowState, error) {
		state.Output = fmt.Sprintf("Processed: %s, Validated: %v", state.Processed, state.Validated)
		state.StepCount++
		return state, nil
	})

	// Define flow
	g.SetEntryPoint("process")
	g.AddEdge("process", "validate")
	g.AddConditionalEdge("validate", func(ctx context.Context, state WorkflowState) string {
		if state.Validated {
			return "output"
		}
		return "process" // Retry
	})
	g.AddEdge("output", graph.END)

	// Compile and run
	runnable, _ := g.Compile()

	result, _ := runnable.Invoke(ctx, WorkflowState{
		Input: "hello world",
	})

	fmt.Printf("Result: %+v\n", result)
	fmt.Printf("Steps: %d\n", result.StepCount)
}

Best Practices

  1. Choose the right agent type for your use case - ReAct for general tasks - Supervisor for multi-agent workflows - PTC for complex tool interactions

  2. Use typed graphs when possible for better type safety

  3. Implement proper error handling in all node functions

  4. Add checkpoints for long-running or critical workflows

  5. Use appropriate memory strategy for conversations

  6. Monitor execution with listeners and logging

  7. Test thoroughly with various input scenarios

Configuration

The library supports configuration through environment variables:

  • OPENAI_API_KEY: OpenAI API key for LLM access
  • LANGGRAPH_LOG_LEVEL: Logging level (debug, info, warn, error)
  • LANGGRAPH_CHECKPOINT_DIR: Default directory for checkpoints
  • LANGGRAPH_MAX_ITERATIONS: Default max iterations for agents

Community and Support

Contributing

We welcome contributions! Please see:

  • CONTRIBUTING.md for guidelines
  • CODE_OF_CONDUCT.md for community standards
  • Examples in ./examples for reference implementations

License

This project is licensed under the MIT License - see the LICENSE file for details.

Directories

Path Synopsis
Package adapter provides integration adapters for connecting LangGraph Go with external systems and frameworks.
Package adapter provides integration adapters for connecting LangGraph Go with external systems and frameworks.
goskills
Package goskills provides an adapter for integrating GoSkills with LangGraph Go agents.
Package goskills provides an adapter for integrating GoSkills with LangGraph Go agents.
mcp
Package mcp provides an adapter for integrating Model Context Protocol (MCP) tools with LangGraph Go agents.
Package mcp provides an adapter for integrating Model Context Protocol (MCP) tools with LangGraph Go agents.
Package graph provides the core graph construction and execution engine for LangGraph Go.
Package graph provides the core graph construction and execution engine for LangGraph Go.
llms
Package log provides a simple, leveled logging interface for LangGraph Go applications.
Package log provides a simple, leveled logging interface for LangGraph Go applications.
Package memory provides various memory management strategies for conversational AI applications.
Package memory provides various memory management strategies for conversational AI applications.
Package prebuilt provides ready-to-use agent implementations for common AI patterns.
Package prebuilt provides ready-to-use agent implementations for common AI patterns.
Package ptc (Programmatic Tool Calling) provides advanced tool execution capabilities for LangGraph Go agents.
Package ptc (Programmatic Tool Calling) provides advanced tool execution capabilities for LangGraph Go agents.
rag
RAG (Retrieval-Augmented Generation) Package
RAG (Retrieval-Augmented Generation) Package
showcases
Package store provides storage implementations for persisting LangGraph checkpoints and state.
Package store provides storage implementations for persisting LangGraph checkpoints and state.
file
Package file provides file-based checkpoint storage implementation.
Package file provides file-based checkpoint storage implementation.
memory
Package memory provides in-memory checkpoint storage implementation.
Package memory provides in-memory checkpoint storage implementation.
postgres
Package postgres provides PostgreSQL-backed storage for LangGraph Go checkpoints and state.
Package postgres provides PostgreSQL-backed storage for LangGraph Go checkpoints and state.
redis
Package redis provides Redis-backed storage for LangGraph Go checkpoints and state.
Package redis provides Redis-backed storage for LangGraph Go checkpoints and state.
sqlite
Package sqlite provides SQLite-backed storage for LangGraph Go checkpoints and state.
Package sqlite provides SQLite-backed storage for LangGraph Go checkpoints and state.
util
Package util provides common utilities for checkpoint store implementations.
Package util provides common utilities for checkpoint store implementations.
Package tool provides a collection of ready-to-use tools for LangGraph Go agents.
Package tool provides a collection of ready-to-use tools for LangGraph Go agents.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL