Skip to content

LangGraph/LangChain Integration

This guide demonstrates how to use Sequrity with LangGraph and LangChain.

Installation

pip install sequrity langchain-openai langchain-core langgraph

Basic Setup

Create a Sequrity-enabled LangGraph client:

# Create Sequrity LangGraph client
llm = create_sequrity_langgraph_client(
    sequrity_api_key=os.environ["SEQURITY_API_KEY"],
    features=FeaturesHeader.dual_llm(),
    security_policy=SecurityPolicyHeader.dual_llm(),
    service_provider="openrouter",
    llm_api_key=os.environ["OPENROUTER_API_KEY"],
    model="gpt-5-mini",
)

Define Tools

@tool
def search(query: str) -> str:
    """Search for information."""
    return f"Search results for: {query}"


@tool
def calculator(expression: str) -> str:
    """Evaluate a mathematical expression."""
    try:
        result = eval(expression)  # noqa: S307
        return f"Result: {result}"
    except Exception as e:
        return f"Error: {e}"

Build Graph

# Define state schema
from typing import Annotated, TypedDict

from langgraph.graph.message import add_messages


class State(TypedDict):
    """State for the agent."""

    messages: Annotated[list, add_messages]


# Bind tools to LLM
tools = [search, calculator]
llm_with_tools = llm.bind_tools(tools)


# Define chatbot node
def chatbot(state: State):
    """Chatbot node that calls the LLM."""
    return {"messages": [llm_with_tools.invoke(state["messages"])]}


# Build the graph
graph_builder = StateGraph(State)  # type: ignore[arg-type]  # TypedDict is valid for langgraph StateGraph
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_node("tools", ToolNode(tools))
graph_builder.add_conditional_edges("chatbot", tools_condition)
graph_builder.add_edge("tools", "chatbot")
graph_builder.add_edge(START, "chatbot")

# Compile the graph
graph = graph_builder.compile()

Run the Graph

def run_example():
    """Run the example."""
    from langchain_core.messages import HumanMessage

    # Run the graph
    events = graph.stream({"messages": [HumanMessage(content="Search for LangGraph and calculate 2+2")]})

    # Print results
    for event in events:
        for value in event.values():
            if "messages" in value:
                message = value["messages"][-1]
                print(f"\n{message.type}: {message.content}")

Complete Example

See the complete working example at examples/control/integrations/langgraph_basic.py.

See Also