Context Engineering for LLM Applications

By Rajit Khosla··5 min read

Introduction

Building an LLM application is not just about sending a prompt and getting a response. The model also needs the right information to answer correctly.

As applications grow, they interact with more data, tools and longer conversations. Managing all this information becomes a challenge. Sending too much information can increase costs and response time, while sending too little can lead to incorrect answers.

This is where context engineering becomes important.

Too little context
You

What is your refund policy for annual plans?

Chatbot

I'm not sure. Could you share the policy details?

You

Isn't that something you should already know?

Chatbot

I would if it had been included in my context.

In our previous article, we explored how to maintain conversation history using OpenAI and Claude. In this article, we'll look at how to select, organize and manage the information an LLM needs in production applications.

What is context engineering?

Context is all the information an LLM receives to generate a response. It can include:

  • System instructions and rules.
  • Conversation history.
  • Retrieved documents and knowledge.
  • Tool results and API responses.
  • Additional information about the current task.

Context engineering is the process of designing and managing this information so the LLM gets what it needs at the right time.

It goes beyond writing good prompts. It involves deciding what information to include, how to retrieve it, how to organize it and when to remove or summarize it.

The goal is simple: provide the right information without overwhelming the model.

Understand the context window

Every LLM has a limit on how much information it can process in a single request. This limit is called the context window and is measured in tokens.

As context grows, the model may find it harder to focus on important information. This is sometimes called context rot.

The solution is not always to increase the context window. It is to manage the information more effectively.

Five practical ways to manage context

  1. 1

    Keep instructions clear and separate

    Keep system instructions separate from conversation history. This makes it easier to maintain the assistant's role, rules and expected behaviour.

  2. 2

    Summarize older conversations

    Avoid sending the entire history every time. Keep recent messages and summarize older ones while preserving important information.

  3. 3

    Retrieve relevant information when needed

    Don't send all your documents to the LLM. Use RAG to find and include only the information relevant to the current question.

  4. 4

    Keep tool results small

    Tools and APIs can return more than the LLM needs. Include only the relevant fields and results to reduce unnecessary data.

  5. 5

    Prioritize information and manage token budgets

    Decide how much context each request can contain. Give priority to important instructions and relevant information, and reserve enough space for the model's response.

Monitor and improve context post release

Context engineering does not stop when the application is released.

Monitor a few important areas:

  • Token usage: How much context is being sent?
  • Response time: How long does the LLM take to respond?
  • Cost: How much does each request cost?
  • Retrieval quality: Are the retrieved documents relevant?
  • Answer quality: Does the context provide enough information for accurate responses?

Reviewing these metrics helps identify whether the application is sending too much information, missing important details or retrieving irrelevant content.

Conclusion

Building reliable LLM applications requires more than good prompts and stored conversation history. It requires a strategy for selecting, organizing and maintaining the information the model receives.

Good context engineering helps make LLM applications more reliable, efficient and cost-effective.

Additional Topic

Summarization vs compaction

When working with long conversations, you may need to reduce the amount of information sent to the model.

Two useful techniques are summarization and compaction.

SummarizationCompaction
What it doesCreates a shorter version of older conversation messagesReduces the size of the existing context
What it preservesImportant details from the conversationInformation needed for the next steps
Who does the workYour application calls the LLM to write the summaryThe platform (OpenAI or Claude) does it for you

You can summarize the messages older than the last 5 messages and send that summary with the recent ones.

For compaction, modern LLM platforms including OpenAI and Anthropic (Claude) provides built-in support.

compaction_openai.py
python
response = client.responses.create(
  model="gpt-5.5",
  input=conversation,
  context_management=[
      # Compact automatically once the context passes 200,000 tokens
      {"type": "compaction", "compact_threshold": 200000}
  ]
)

# Pass the output items (including the compaction item)
# back as part of the next request's input.
conversation += response.output

Want to build practical AI applications?

Auragile's AI Engineering course teaches developers by building real-world projects, from LLM-powered applications to more advanced AI systems.

Explore the course →

Get new articles by email

Get new Auragile articles and course news by email. Unsubscribe anytime. See our Privacy Policy.