Skip to content
All posts

Managing context in LLM apps: long conversations, long documents and what to leave out

How to manage the context you send to an LLM: trimming and summarising long conversations, handling long documents, keeping memory under control and testing what the model actually sees.

Shubham Somani 4 min read

Every request to an LLM is built from scratch. The model doesn’t remember your user, the last message or the document they uploaded yesterday. Whatever you put in the context for this request is all it knows.

That makes context one of the most important design decisions in an AI feature, and one of the least visible. Send too little and answers are vague. Send too much and the feature gets slow, expensive and sometimes less accurate. Here’s how we’d manage context in an LLM app.

Know what’s in the context, every time

Most context problems start with nobody being sure what the model received.

  • Build the context in one place in your code, from clearly named parts: instructions, user details, retrieved documents, conversation history, the current message.
  • Log the assembled context for a sample of requests, with care for personal data, so you can see exactly what the model saw when an answer goes wrong.
  • Track its size per part. When cost or response time creeps up, you’ll know which part grew.

Long conversations: don’t resend everything forever

Chat features usually send the whole conversation on every turn. That’s fine for five messages, and a problem for fifty.

  • Keep the last few turns in full. Recent messages matter most for what the user means right now.
  • Summarise older turns into a short running summary, updated as the conversation goes on.
  • Pin the facts that must survive, such as the user’s goal, decisions already made and key details they gave, separately from the summary so they don’t get lost.
  • Start fresh when the topic changes. A new question rarely needs an hour of unrelated history.

Test the summary itself. If it drops something important, the model will act as if it was never said.

Long documents: retrieve, don’t paste

Putting a whole document in every request is tempting, especially when it fits. It’s often the slower, costlier and less reliable choice.

  • Split documents into sections and retrieve the ones relevant to the question. Our post on why RAG stops working covers chunking and retrieval in detail.
  • Use the whole document when the task really needs it, such as summarising it or comparing every clause, and measure the cost of doing so.
  • Put the most important material where the model handles it best for your use case, and test placement rather than assuming. Details buried in a long context can be missed.

Memory across sessions: be deliberate

“Remember things about the user” sounds simple and isn’t.

  • Store memory as structured facts in your database, such as preferences or account details, rather than raw old conversations.
  • Decide what’s worth remembering and let users see, correct and delete it.
  • Retrieve only the memory relevant to the current request, the same way you’d retrieve documents.
  • Scope it strictly to the user. One user’s memory appearing in another’s answer is a serious data leak. See keeping customer data safe in LLM features.

Leave things out on purpose

More context isn’t always better. Each extra item is something the model might focus on instead of what matters.

  • Drop tool outputs and intermediate steps once they’ve been used, keeping only the result.
  • Keep secrets out entirely. Anything in the context can end up in an answer.

Shorter context also helps with speed and cost. Why your LLM feature is slow and what an AI feature costs to run go into both.

Test with long, messy sessions

  • Add long conversations and large documents to your test set, not only short, clean examples.
  • Check that key facts from early on still shape answers late in a conversation.
  • Re-run the tests whenever you change how context is built. We cover how in test your LLM feature before users do.

The short checklist

  • Build context in one place and log what the model actually sees
  • Keep recent turns, summarise older ones and pin key facts
  • Retrieve relevant sections instead of pasting whole documents
  • Store memory as user-scoped facts that users can see and delete
  • Leave out stale instructions, used tool output and secrets
  • Test long, messy sessions on every change

If your AI feature gets worse as conversations or documents get longer, see our fixed-scope offers or book a 30-minute call.