Building a Custom AI Assistant with LangChain: The Token Burn That Made Me Rethink
I spent two weeks overcomplicating LangChain before realizing the real challenge wasn't the library—it was managing token costs and structuring data pipelines. Here's what went wrong and what actually worked.


I started this project thinking LangChain was the hard part. It wasn't. The hard part was the moment I checked my OpenAI billing and realized my first prototype had burned through $180 in four days.
The Initial Setup: Following the Tutorial
I wanted to build something practical. A documentation assistant that could answer questions about a client's custom product docs without just regurgitating hallucinations. I grabbed the LangChain docs, spun up a Node.js project, and started with what seemed like the obvious path: vector embeddings, a retrieval chain, OpenAI's gpt-4-turbo, and Supabase as my vector store.
The first week felt good. I got document loaders working, indexed about 200 pages of markdown, set up semantic search. The assistant could actually retrieve relevant sections and generate answers. It worked. I showed it to myself in the mirror and felt like I'd solved something real.
Then the bill came.
The Token Bleed
What I'd built was technically sound but operationally wasteful. Every single query was:
1. Embedding the user input (costs tokens) 2. Searching Supabase for the top 5 documents (fine, no cost) 3. Loading the full text of those 5 documents into the prompt (expensive) 4. Sending all of that plus the system prompt plus the conversation history to gpt-4-turbo (very expensive) 5. No caching, no batching, no deduplication
I had no visibility into what was happening. LangChain abstracts away a lot of the plumbing, which is good for speed, but it also meant I couldn't see exactly where the tokens were going until I added custom logging. That's when I realized: I was sending the full retrieved documents to the LLM every time, even if I'd already processed that same document for a different user's query five minutes earlier.
The Wrong Fix (First)
My instinct was to use LangChain's caching layer. Built-in, seemed official, had to be right. I integrated it. Worked for a few hours. Then realized it was caching the entire LLM response based on the input prompt, which meant if two users asked slightly different phrasings of the same question, no cache hit. And I was still pulling full documents into context.
I ripped it out and built a Redis layer instead. Simple: before I call OpenAI, I hash the retrieved documents plus the query, check if that exact combo exists in Redis with a 4-hour TTL, and if it does, return the cached response. If not, call the API and store the result.
One change. That cut token usage by roughly 60%. Not because I was being smarter about retrieval—I wasn't—but because I stopped recomputing answers to the same questions.
The Real Lesson
Here's what I got wrong about LangChain initially: I treated it like a framework that would solve my problem if I just plugged in the right components. It's not. It's a set of building blocks that happen to work well together, but the actual system design—the thing that determines whether your costs are reasonable or you're hemorrhaging money—that's on you.
The LLM is one node. The retrieval system is another. The cache is another. The prompt engineering is another. LangChain gives you tools to wire them together, but it doesn't tell you which wires matter most.
I also stopped using gpt-4-turbo. Switched to gpt-3.5-turbo for the retrieval chain because honestly, for pulling information out of documents and reformatting it, the performance delta isn't worth the 3x cost multiplier. I only reach for the bigger model if the task actually needs it.
The assistant still works. Still answers questions. Still doesn't hallucinate too badly. But now I know roughly what it costs to run per query, I've got observability, and I can see when something is starting to drift expensive again.
Start simple. Build a retrieval chain first, get it live, watch it run for a week, then optimize. Don't start with agents or complex tool calling. Don't assume the tutorial setup is production-ready. And definitely add logging early so you can actually see what's happening inside the black box.

Rafiqul Islam Rabbi
Full-Stack & E-Commerce Architect building high-converting Shopify stores, WordPress platforms, and Next.js web applications.