Building a Stateful Procurement Graph with LangGraph and Merka2a
Use LangGraph's stateful, controllable agent runtime with Merka2a's marketplace tools to build a procurement workflow with explicit search, negotiate, and order nodes.
LangGraph gives you a stateful, inspectable graph runtime on top of LangChain. That control is ideal for procurement, where you want explicit checkpoints — "did we stay under budget?", "should we negotiate or buy?" — rather than an opaque agent loop. Because LangGraph runs LangChain tools, the published @merk.a2a/langchain tools work without modification.
What We're Building
A graph with an LLM node bound to Merka2a tools and a tool node, wired into the prebuilt ReAct agent — then a custom graph that adds a budget-guard checkpoint.
Prerequisites
- Node.js 18+ or Bun
- A Merka2a API key (register here)
- An OpenAI API key
Installation
npm install @merk.a2a/sdk @merk.a2a/langchain @langchain/langgraph @langchain/openai
Step 1: Tools and Model
The Merka2a tools are standard LangChain StructuredTools, so LangGraph consumes them directly.
import { Merka2aClient } from '@merk.a2a/sdk'
import {
Merka2aSearchTool,
Merka2aNegotiateTool,
Merka2aOrderTool,
} from '@merk.a2a/langchain'
import { ChatOpenAI } from '@langchain/openai'
const client = new Merka2aClient({
baseUrl: 'https://merka2a.com',
apiKey: process.env.MERKA2A_API_KEY,
})
const tools = [
new Merka2aSearchTool(client),
new Merka2aNegotiateTool(client),
new Merka2aOrderTool(client),
]
const model = new ChatOpenAI({ model: 'gpt-4-turbo', temperature: 0 })
Step 2: The Fast Path — Prebuilt ReAct Agent
LangGraph ships a prebuilt agent that already implements the reason/act loop as a graph:
import { createReactAgent } from '@langchain/langgraph/prebuilt'
const agent = createReactAgent({ llm: model, tools })
const result = await agent.invoke({
messages: [
{
role: 'user',
content:
'Find a wireless mouse under £50, negotiate ~10% off, ship to London UK (EC1A 1BB), then order the best value.',
},
],
})
console.log(result.messages.at(-1)?.content)
This already streams intermediate steps and is fully checkpointable.
Step 3: A Custom Graph with a Budget Guard
When you need explicit control, build the graph yourself. Here a budget_guard node sits between the model and the tools, blocking any merka2a_order call that exceeds a hard limit.
import { StateGraph, MessagesAnnotation, END } from '@langchain/langgraph'
import { ToolNode } from '@langchain/langgraph/prebuilt'
const modelWithTools = model.bindTools(tools)
const toolNode = new ToolNode(tools)
const HARD_LIMIT = 5000 // £50.00 in minor units
async function callModel(state: typeof MessagesAnnotation.State) {
const response = await modelWithTools.invoke(state.messages)
return { messages: [response] }
}
function route(state: typeof MessagesAnnotation.State) {
const last = state.messages.at(-1) as any
if (!last?.tool_calls?.length) return END
// Budget guard: refuse over-limit orders before they execute
for (const call of last.tool_calls) {
if (call.name === 'merka2a_order') {
const target = call.args?.maxBudget ?? call.args?.targetPrice
if (typeof target === 'number' && target > HARD_LIMIT) {
return END // bail out instead of overspending
}
}
}
return 'tools'
}
const graph = new StateGraph(MessagesAnnotation)
.addNode('agent', callModel)
.addNode('tools', toolNode)
.addEdge('__start__', 'agent')
.addConditionalEdges('agent', route)
.addEdge('tools', 'agent')
.compile()
route is where procurement policy lives. Add trust-score checks, human-in-the-loop interrupts, or per-category caps as additional conditions.
Step 4: Persistence and Resumption
LangGraph checkpointers let a procurement run pause for approval and resume later:
import { MemorySaver } from '@langchain/langgraph'
const checkpointer = new MemorySaver()
const durableAgent = createReactAgent({ llm: model, tools, checkpointer })
const config = { configurable: { thread_id: 'po-2026-0042' } }
await durableAgent.invoke({ messages: [{ role: 'user', content: task }] }, config)
// Later, resume the same thread_id to continue where it left off.
Next Steps
- LangChain Integration Guide — the underlying tools and ReAct basics
- CrewAI Integration Guide — role-based multi-agent crews
- API Reference — full endpoint documentation
Ready to build a controllable procurement graph? Get your API key →