LangChain vs LangGraph in ASP.NET Core 10: Build a Support Ticket Retry Workflow

Use LangChain when you want to compose operations such as calling a model and formatting its output. Use LangGraph when the next step depends on the current workflow state. This tutorial runs the same fictional support ticket through both. The LangChain example reads the service status once. The LangGraph example remembers how many times it checked and retries a temporary failure once before escalating.
Imagine the on-call queue getting a ticket because the VPN status page briefly stopped responding. On the first read, the service looks unavailable. The second read succeeds. Escalating after the first read creates work for someone who can’t reproduce it. Retrying forever keeps the caller waiting. A two-attempt limit makes both outcomes predictable.
What is the difference between LangChain and LangGraph?
| Question | LangChain composition in this lab | LangGraph workflow in this lab |
|---|---|---|
| What happens next? | The probe runs once, then a formatter runs. | A conditional edge chooses resolve, retry, or escalate. |
| Where is the attempt count? | The caller supplies the first-attempt constant. | The graph carries attempts in its state. |
| Does it need a model? | No. RunnableLambda wraps ordinary Python functions. | No. StateGraph can orchestrate ordinary Python functions. |
| Can they work together? | A runnable can perform a step such as structured extraction. | A node can invoke that runnable and route from its result. |
LangChain can also express branches and its higher-level agents are built on LangGraph. The contrast here is about which abstraction you reach for first. A fixed, two-step transform reads well as a chain. A loop with explicit business outcomes reads well as a graph. If the production rule remains a lone two-attempt loop, plain Python is also a reasonable choice.
How does the ticket move through the graph?
Client → ASP.NET Core → HTTP → Python FastAPI → check service
- OK → resolved → end.
- Temporary failure and one attempt left → check service again.
- Missing service or attempt budget exhausted → escalated → end.
check increments attempts before route runs. Each node returns a partial state update. The router reads the updated state and its return type lists possible destinations. Both terminal nodes are connected to END. There is no checkpointer in this synchronous example. A second request starts a new graph invocation.
Build and test the Python worker
Use Python 3.12 and the pinned packages below. Put worker.py and requirements.txt together. The fixture is deterministic and requires no model account, credentials, or external service. The scenario field exists to drive the lab’s fake backend.
Windows PowerShell
macOS or Linux
Call the workflow from ASP.NET Core 10
Open a second terminal. Create a minimal project with dotnet new web -n TriageGateway -f net10.0, then replace TriageGateway/Program.cs with the code below. The two HTTP addresses assume both processes run on the same machine. If you containerize them later, configure the worker’s address to use its internal service name because 127.0.0.1 would point back to the gateway container.
What should each test return?
| Input | Worker result | What it proves |
|---|---|---|
| healthy | resolved, 1 | A good first read stops immediately. |
| recovers | resolved, 2 | The graph retries once and reuses its attempt count. |
| offline | escalated, 2 | Two temporary failures consume the budget. |
| unknown | escalated, 1 | A missing service skips the retry. |
| other | HTTP 422 from FastAPI | The worker rejects a value outside the contract. |
On Python 3.12, the script passed five assertions, including the one-pass LangChain result. Five additional FastAPI TestClient checks passed across the four routes and invalid input. The .NET SDK was unavailable in the verification environment, so the gateway was reviewed but not executed. Its expected result for recovers is HTTP 200 with status: resolved, attempts: 2, and a gateway trace ID.
Troubleshoot and prepare for production
| Symptom | First check | Fix |
|---|---|---|
| Gateway returns 502 | Is Uvicorn listening on port 8000? | Run the Python worker and confirm the gateway URI. |
| Gateway returns 504 | Is the worker finishing inside the five-second HTTP budget? | Set a smaller deadline per dependency call and keep total attempts bounded. |
| Unknown service is retried | Does the router classify a missing record separately? | Escalate known permanent outcomes on the first attempt. |
| Duplicate tickets appear after retry | Is the retried node writing to a queue? | Use an idempotency key or transactional outbox for real enqueue operations. |
Here “escalated” is a result for the caller. The example doesn’t write a support ticket. In production, keep the retry around read-only checks where possible. Real network retries need a total deadline and usually backoff. If you also retry at the HTTP gateway, count the combined attempts across both layers. Map worker connection faults to 502 and timeouts to 504. A completed escalation still returns 200 because the workflow reached a valid outcome. For multi-request workflows, add persistence and handle state ownership separately.
Frequently asked questions
Can I use LangGraph without LangChain?
You can write application nodes as plain Python functions without creating a LangChain chain or agent. LangGraph itself uses packages from the LangChain ecosystem.
Can a LangGraph node run a LangChain runnable?
Yes. For example, run a structured extraction step inside a node, validate the returned fields, then let the graph decide whether to retry or escalate. Keep deterministic business rules in application code.
Does running two attempts require a checkpointer?
No. Both attempts happen within a single invocation in this example. A checkpointer is relevant when you need persisted state for features such as resuming a workflow across requests.
How does this relate to the ASP.NET PB RAG tutorials?
Both patterns keep Python behind an ASP.NET Core boundary. In the citation verification tutorial, the worker returns evidence to validate. Here, the worker returns a routing outcome. For workflow pauses across requests, see the LangGraph approval and checkpoint walkthrough.
Comments
Post a Comment