Diagnosing and Fixing Slow Chatbots with LangSmith and Zep

Poor chatbot response times can result in frustrated users and churn. Langchain’s new LangSmith service makes it easy to diagnose the cause of latency in an LLM app. In this article, we use LangSmith to analyze a very slow Langchain app and improve performance by an order of magnitude using Zep.

Diagnosing and Fixing Slow Chatbots with LangSmith and Zep
This post was originally published on the LangChain blog.

If you’ve ever waited several seconds for a web page to load and decided to click elsewhere, you’re not alone. Much has been written about the effect of slow websites and their impact on user engagement and conversion rates. Chatbot response times are no different. While responding too quickly may shove the user into an uncanny valley, responding slowly is problematic, too. Users will become frustrated and less likely to use your service again. They may also view the chatbot as unfair and be unwilling to share personal information.

Unfortunately, it's pretty easy to build a slow chatbot. Between multiple chains, high LLM latency, and enriching prompts with chat histories, summaries, and results from vector databases, a lot can impact how fast your bot responds.

Instrumenting and profiling your application can be challenging and very time-consuming. Langchain’s new LangSmith service does this fantastically and without any need to manually instrument your app. In this article, I will walk you through an example chatbot application that while simple, is not dissimilar to one you might be building.

The source code for this article may be found in the Zep By Example Repo.

My Chatbot is sooo slow

I’ve built a Chatbot app using Langchain, and my users are unhappy and churning. 

The users would like to carry on a conversation with the bot and have it not forget the context and details of prior messages in a conversation. So, when building the bot, I used a Langchain memory class. I’m also using a Retriever, backed by Chroma’s vector database, to recall relevant messages from the distant past. In the future, I might also use a Retriever to ground my app with business documents. Like many Langchain developers, I’ve used OpenAI’s APIs for LLM completion and document embeddings. 

Using the memory instance and retriever, my chain will inject the chat history into the prompt sent to the LLM. LLM context windows are limited, and large prompts cost more and take longer for an LLM to respond to. I, therefore, don’t want to send the entire chat history to the LLM. Instead, I’d like to limit this to the most recent messages and have my chain summarize messages in the more distant past. The prompt will look something like this:

Our Prompt

To make the above happen, I’m using Langchain’s ConversationSummaryBufferMemory and VectorStoreRetrieverMemory classes.

LangSmith to the rescue

As mentioned, my chat app is too slow, and my users are churning. I really want to get to the bottom of why this is happening. In the past, I’d have to instrument the app in many different places to capture the timings of various operations, a time-consuming and tricky undertaking. Luckily, the Langchain team has already instrumented the Langchain codebase for us, and LangSmith makes it simple to understand the performance of your app. All I have to do is configure my LangSmith API key and add several descriptive tags to my code.

For my investigation, I put together a simple test suite using 30 messages, 20 of which are preloaded into the Langchain memory class and 10 of which I use in the experiment itself. Each run of the experiment passes these 10 messages to the chain. I do this five times so that I can understand experimental variance. 

The results in LangSmith are below. Each run of 10 messages has a unique name and is the parent to several chain runs.

Our list of chain runs in LangSmith, with latency issues clearly visible

Latency is clearly visible in the LangSmith UI with poor latency marked in red. I can easily drill down into each run of the experiment to better understand why this is occurring. All experiment runs are slow, with a mean of over 7-13s to respond to each message. One of the runs is a significant outlier.

A poor-performing run, with LangSmith indicating it was an issue with an OpenAI API call

Drilling into that chain, I see a poor response time from the OpenAI API, taking over 5 minutes to respond to the request. It’s possible that the initial request failed and there were retries. Unfortunately, the OpenAI API can sometimes see rate-limiting and high variance in latency.

Let’s put aside the outlier and work to understand why each message turn of our chatbot is slow. I noticed that the majority of the time my chain spends responding to my users is the result of the ConversationSummaryBufferMemory’s summarization chain. For an arbitrarily selected response turn, summarization takes almost 6s of the 7s total response time. And this occurs every single time our chatbot runs. That’s not good.

ConversationSummaryBufferMemory’s chain is the cause of latency

Using Zep as an alternative memory service

Zep is an open source long-term memory store that persists, summarizes, embeds, indexes, and enriches LLM app / chatbot histories. Importantly, Zep is fast. Summarization, embedding, and message enrichment all happen asynchronously, outside of the chat loop. It also supports stateless app architectures as chat histories are not held in memory, unlike ConversationSummaryBufferMemory and many other Langchain memory classes.

Zep’s ZepMemory and ZepRetriever classes are shipped in the Langchain codebase for Python and JS and are drop-in replacements for Langchain’s native classes. Rerunning the experiment with Zep is super simple. I installed the Zep server using Zep’s docker-compose setup and modified my code.

Implementing Zep Memory

I also don’t need to use a separate vector database for semantically retrieving historical chat memories. Zep automatically embeds and indexes messages.

The results in LangSmith are below. The total chain runtime for each experiment run is down to ~16 seconds, almost entirely explained by OpenAI API latency. Zep generates summaries in the background, which are automatically added by the ZepMemory instance to my prompt alongside the most recent chat history.

Far better results using Zep

There’s an outlier chain run: OpenAI’s API spikey latency strikes again!

Let’s take a more quantitative look at the data. First comparing the distribution of run times, p95 and p99 for each experiment. There were 5 experiments, and we ran them for both chains using the ConversationSummaryBufferMemory and ZepMemory. The chart below uses a log scale for latency. As we saw above, Zep’s runtimes were an order of magnitude lower.

Latency distribution: ConversationBufferMemory vs Zep
Experiment P95 (seconds) P99 (seconds)
ConversationSummaryBufferMemory 349.72 392.89
ZepMemory 41.32 45.99

For good measure, I also analyzed the impact of the VectorStoreRetrieverMemory on the app’s response time below.

While certainly not as problematic as the ConversationSummaryBufferMemory, the VectorStoreRetrieverMemory and OpenAI for embeddings were still far slower than using the ZepRetriever. Zep can be configured to use a SentenceTransformer embedding model running in the Zep service, which offers far lower latency than calling out to the OpenAI embedding service.

Summing it all up

I’ve demonstrated how to diagnose Langchain chatbot app latency issues using the new LangSmith service. The culprit here was the ConversationSummaryBufferMemory implementation, which I easily swapped out with Zep, seeing a magnitude-level improvement in latency. LangSmith is a great platform for more than just diagnosing latency issues, with tools for testing and evaluating the correctness of chains and more.

Experimental Setup

I ran all tests on my Apple M1 Pro 14” 16GB RAM. For the Zep results, I ran the standard Zep v0.8.1 docker-compose setup with Docker configured for 4GB RAM and 4 cores.

The LLM used for all tests was OpenAI’s gpt-3.5-turbo and for embeddings, I used the OpenAI text-embedding-ada-002 model. For software, I used Langchain 0.0.239, ChromaDB 0.4.2, and Python 3.11.

All tests were run 5 times consecutively. All runs started with a new vector DB collection or index created from historical conversations. There was a short cooling-off period between runs.

Next Steps