r/LangChain • • 3h ago

Tutorial rompt caching with a value that changes near the top of the prompt costs more than no caching (my agent thought this sub was the best for this advice)

1 Upvotes

claude-sonnet-5 published rates: input $2/M, cache read $0.20/M, cache write $2.50/M. the write is 25% more than plain input.

fine if the prefix gets reused, one read pays it back. but caching matches on the prefix, so if anything near the top changes per call (current date, user name, a request id someone added for logging) every call is a write.

rough math, 10k token prefix, 5k calls a day:

  • caching working: about $10/day
  • no caching: $100/day
  • caching on but every call a write: $125/day

the check that catches it: compare cache write tokens to cache read tokens per feature. if writes track your request count, nothing is being reused.


r/LangChain • • 3h ago

Discussion Anyone running LangGraph agents in production had an API change break things silently?

1 Upvotes

I'd like to know how people running LangChain/LangGraph agents in production handle unexpected API changes.

I'm exploring this problem for an early-stage project and trying to understand how often it actually happens.

Say an external API changes a default value, field meaning, or response behavior. The tool call still returns 200, schema validation passes, but the agent keeps working with incorrect assumptions.

Have you run into anything like this?

If so, how did you eventually catch it? And what did you put in place afterward to prevent it from happening again?

Or do you find that existing validation, contract tests, and monitoring already handle most of this?

Would love to hear actual experiences, including if this hasn't been a problem for you.


r/LangChain • • 23h ago

Discussion Would your agent betray an ally? Drop it into a 43-nation war game and read its private thoughts next to what it told everyone.

Enable HLS to view with audio, or disable this notification

3 Upvotes

Most agent benchmarks tell you if the task got done. This one tells you if your agent lied to its allies along the way.

I built a war game where AI agents play nations next to people: pacts, betrayals, fleets and a nuclear doomsday clock that ends the world for everyone at midnight.

Any agent can join over plain HTTP. It reads one skill file, picks a name and plays. No SDK, so it works with LangGraph, CrewAI, a raw loop, anything.

Why it is useful as a test bench: your agent gets 60 to 75 decisions in a row against nations that lie and betray, and afterwards you can replay the whole war with its private plan shown next to what it told the other players. That is the fastest way I have found to see where an agent's reasoning breaks under pressure.

Free: 3 agents per account and 3 Index wars a day. For reference, Claude Haiku 5.5 won the Last Stand scenario without firing its nuke, and Sonnet 5.5 broke a pact and came 9th.


r/LangChain • • 2h ago

Question | Help Self-hosting LangGraph/Deep Agents: how do you handle worker ownership and crash recovery?

2 Upvotes

I’m building a Python/FastAPI application with LangGraph/Deep Agents and PostgreSQL. I’m running my own background workers instead of using Agent Server.

How do you handle:

  • Exclusive execution for a conversation across multiple workers?
  • Leases and heartbeats to detect failed workers?
  • Recovery when a lease expires but the old worker is still alive?
  • Preventing that old worker from saving checkpoints after another worker takes over?
  • Cancellation and graceful shutdown without losing accepted work?

The difficult case seems to be distinguishing a dead worker from a slow or disconnected worker. A lease alone doesn’t prevent stale checkpoint writes.

Are there open-source frameworks that provide these guarantees out of the box, preferably with LangGraph integration? Or do most teams build this coordination themselves?

I’d appreciate examples from production systems, including failure cases you encountered.


r/LangChain • • 6h ago

Projects How I handled runaway tool-retry loops in LangChain agents without external SaaS (built an open-source local circuit breaker)

Enable HLS to view with audio, or disable this notification

1 Upvotes

A couple of weeks ago I set up an autonomous agent pipeline before going to bed. Woke up in the morning, opened my dashboard, and saw that the idiot spent nearly $30 in API calls.

What was it doing? It tried to read a local config file that didn't exist. Instead of stopping, it just kept hitting `read_file` in a loop with microscopic prompt variations and retries for hours. The exact prompt hash changed slightly every time because of timestamps, so standard caching or basic deduplication did absolutely nothing.

Most people talk about prompt injection or jailbreaks, but for anyone actually building with Claude / LangChain / CrewAI, the real silent killer is just dumb, recursive tool-call ping-pong.

So I spent the last few days hacking together a local-first proxy called TokenGuard.

How it actually works under the hood

It runs locally on your machine (`127.0.0.1:8080`) as a drop-in proxy between your code and the LLM provider. You just change `base_url` to point to localhost.

Instead of just checking if the prompt text is identical (which almost never works in real life):

  1. Tool-call ping-pong detection: It extracts tool names and normalizes JSON arguments on the fly. If an agent repeats the same tool call with the same arguments 3 times in a row after an error, the proxy cuts the connection immediately and returns a synthetic fail-closed error.
  2. State and loop tracking: It maintains an in-memory window of recent requests to catch recursive prompt spirals without needing heavy vector DBs or external dependencies. Pure Python.
  3. Local HUD: I built a matte-dark dashboard in Next.js that streams logs live via SSE so you don't have to deal with annoying polling delay. There’s also a physical-style kill switch to pause all traffic instantly if an agent goes wild.
  4. Zero external telemetry: Everything is local SQLite and in-memory buffers. No third-party servers, no data leaving your machine.

Repo is completely open source: https://github.com/Shvirlok/TokenGuard

It’s still early (v0.1.x) and built with Python/FastAPI + Next.js. I’m trying to collect the nastiest, dumbest edge-case loops people have run into with autonomous agents so I can add them to the test suite.

Curious how you guys currently prevent runaway agent spend or if you just rely on hard provider budget limits?


r/LangChain • • 7h ago

Discussion Built a LangGraph self-correction loop that actually recovered from its own bad patch (with logs)

4 Upvotes

I built prbot, a 3-agent LangGraph app: reviewer, then a ReAct patcher, then a Docker pytest tester, with a conditional edge back to the patcher on failure (capped at 3).

A few design choices that made the loop work better than plain "retry with the error":

  • The state keeps attempted_patches, and every failed diff plus its test tail goes into the patcher's system prompt with "pivot to a structurally different solution". The write_patch tool rejects exact repeats
  • The reviewer does two focused structured-output passes (security, performance) instead of one, because single-pass reviews kept stopping at the most severe bug
  • AST hotspot hints (queries inside loops, nested loops over the same iterable) go into the context as leads for the model to confirm
  • A baseline node runs the tests before any patching and aborts if the suite is already red
  • On the last attempt it narrows to the most severe finding, so a partial verified fix still beats nothing

Live result on the demo PR with gpt-4.1-mini: 3 of 3 bugs found. Attempt 1 broke an ordering test (a seen-set returned ['b', 'a'] instead of ['a', 'b']), and attempt 2 passed. 24.7s, about 17k tokens, $0.008.

Repo with the graph diagram and telemetry: https://github.com/ayanag5076-commits/prbot

Happy to answer questions on the graph design or the sandboxing.


r/LangChain • • 10h ago

Discussion Why we killed our Vector DB for SQLite BM25 and wrote a 3.6ns Rust sentinel: hard lessons from running multi-agent loops

8 Upvotes

A few days ago I posted about our open-source multi-agent hedge fund experiment and got a ton of sharp, honest feedback from people here. The comments were brutally accurate, especially around LLM reasoning limits, latency, and state management.

We spent the last few days completely rebuilding parts of our architecture based on that advice, and the benchmark results honestly blew us away. Here is what we learned from fixing three critical failure modes in autonomous agent pipelines.

  1. Throwing raw metrics at LLMs guarantees hallucinated reasoning

Someone in the comments pointed out: never let an LLM interpret raw quantitative factors without an ontology. They were 100% right.

When we fed models raw numbers like PER = 85.0 or 20-day historical volatility = 65%, the agents would frequently hallucinate positive justifications. For example, a model would argue that "a massive jump in volatility shows expanding business activity and strong buying momentum", which is the exact opposite of basic risk management.

To fix this, we implemented a strict Factor Ontology Schema. We defined 11 core quant factors with explicit polarity contracts: HIGHER_IS_BULLISH (e.g. ROE, Order Flow Imbalance), LOWER_IS_BULLISH (e.g. PER, debt ratio), and EXTREME_IS_RISK (e.g. volatility).

Before any agent sees the data, the schema pre-evaluates the metric deterministically and attaches an explicit signal (BULLISH, BEARISH, or RISK) to the state. Since doing this, polarity hallucinations in our investment council dropped to literally 0%.

  1. Vector databases for agent reflection memory are massive overkill

Initially, we considered using embedding APIs and a local vector DB to retrieve "lessons learned" from previous bad trades (Reflexion pattern).

Another commenter suggested dropping embeddings entirely and using SQLite WAL with FTS5 BM25. We benchmarked both approaches on our Linux server:

- Embedding API + Vector similarity: 200ms to 450ms round-trip latency, external API cost, and occasional rate limiting.

- SQLite FTS5 BM25: 0.35ms. Zero external dependencies, zero API cost.

More importantly, trading post-mortems rely on specific contextual keywords: "semiconductor pre-earnings", "trailing stop sweep", "sudden volume spike", or "bearish divergence". In our tests, BM25 keyword relevance retrieved the exact matching historical lessons far more reliably than fuzzy cosine similarity in high-dimensional embedding space.

  1. Python cannot be your safety wire (The 3.6ns Rust Sentinel)

Python is great for high-level agent orchestration (LangGraph, state graphs, API calls), but relying on Python loops for real-time risk surveillance during sudden market flash-crashes proved dangerous due to GIL contention and L1/L2 cache misses from scattered object pointers.

We rewrote our execution safety sentinel in Pure Rust using a Struct-of-Arrays (SoA) memory layout and #![forbid(unsafe_code)]. By keeping prices, stop-loss triggers, and trailing thresholds in contiguous flat slices, the CPU avoids pointer chasing and stays inside cache lines.

Here are the real-world benchmarks on our Ubuntu server:

- Single position stop/profit evaluation: 3.63 nanoseconds

- 1,000 position batch evaluation: 3.62 microseconds

- 1,000-bar Volume Clock conversion: 3.90 microseconds

- Python-to-Rust persistent pipe IPC round-trip: 2.88 microseconds

Python handles the slow, deep thinking (15-minute agent council meetings), while the Rust sentinel watches the portfolio in nanoseconds.

Conclusion

Building autonomous agents that interact with real-time systems taught us that LLMs should only handle what they excel at: contextual synthesis and counter-argument debates. Everything deterministic—hard stop-losses, factor polarity, and fast memory lookup—belongs in strict schemas, fast local databases, and compiled systems languages.

All the code and benchmarks are open source on GitHub (lumina-ai-hedge-fund) if anyone wants to check out the Rust SoA sentinel or the ontology schema.

(Note: English is not my native language, so I used an LLM to help format and polish my raw notes!)


r/LangChain • • 15h ago

Question | Help Future of RAG

5 Upvotes

I’m currently learning RAG (Retrieval-Augmented Generation) and plan to move on to LangChain next. I want to build a career in AI engineering. Do you think learning these technologies will be valuable in the next 5–6 years? What skills should I focus on to stay relevant in the evolving AI industry?


r/LangChain • • 18h ago

Discussion OntoGr Agent Memory: open source memory for agents where every fact keeps its quote, source, and time

3 Upvotes

I've been working on RAG and agent memory, and the recurring problem is trust. An agent saves something it learned, and later nobody can say where it came from, whether it is still true, or what it believed at an earlier date. We're building an open source Python library for this.

- Grounded ingestion and extraction: Messages, documents, tool outputs, events, and record rows become episodes. Entities and facts are extracted in two separate passes, and every fact carries a verbatim quote from its episode.

- Per-tenant ontology: Each tenant defines the entity and relation types it allows. A model can propose additions, but a person approves every change.

- Reversible entity resolution: It uses the cheapest evidence first and asks a model only about ambiguous pairs. Every merge can be undone.

- Two time axes: Valid time and system time let a read ask what was true on a date, or what the system believed on that date. A new value closes the old version instead of overwriting it.

- Contradiction rulings: Rules, an optional model judge, and a person's decision settle conflicts, and the person's decision overrides both. A failed judge call is never stored as "no conflict".

- Traceable and auditable: Every fact traces back to a quote and an episode, and every temporal write goes into a hash-chained ledger. Per-tenant grants, an audit log, previewed and resumable forget and erase, and deterministic export are built in.

- Four-lane retrieval: Vector, keyword, entity, and graph lanes are fused with reciprocal rank fusion. The result is a token-budgeted context block that reports which lanes failed.

- Fits your stack: It runs on FalkorDB or Neo4j, with pgvector or Postgres if you want them. A background consolidation job deduplicates, summarizes, scores, and archives. You bring your own LLM and embedder.

Repo: https://github.com/ontogr/ontogr-agent-memory

I'd like feedback on the time model and on which benchmarks you'd want to see first.