Loading...

Loading...

Context Optimization: Design Patterns for Cleaner Context
Loading like and share buttons...
#context#llm#coding#generativeai

Context Optimization: Design Patterns for Cleaner Context

Tomer Raitz
Tomer Raitz
Feb 54 min read

Last time, we discussed the Context Window in our "Understanding Context Window" post, where we talked about how important it is to keep a “clean” context. Now, we are going to dive into one of my favourite topics: Context Optimization.

What can be in context?

There are a few options for what can be contained within the context window. Here is the list:

  • System Prompts: These are the default instructions regarding how the conversation should be handled (applicable to all conversations). For example: "Be kind and answer the user with respect."
  • User Prompts: This refers to the input that the user adds to the conversation right now.
  • Prompt History: With every new prompt, the system saves the conversation history. This allows the LLM to understand what the user and the model have previously discussed and to answer questions based on that context.
  • Tools: These are embedded capabilities that allow the AI to perform specific functions, such as searching the web, creating files, modifying files, etc.
  • MCP Tools: MCP (Model Context Protocol) is a standard that allows LLM agents to communicate with each other when they need information or functionality from third-party services. For example, a Google Calendar integration might offer tools to add, delete, or modify events.
  • Docs: We can add documents (files) to the context so the AI understands the specific material we are discussing. For example, we might upload a resume for the AI to review.

Context engineering - design patterns

Let’s explore some design patterns that will improve our context optimization:

  1. The Multi-Agent Routing Pattern

    This is one of the most well-known patterns. Here, we give a specific functionality to an agent and assign it a specific task. In this architecture, we have two main levels:

    • Orchestrator: This is the manager that controls the flow. It gets the full context and assigns each task to a specific agent.
    • Specialists / Sub-agents: These are agents that specialize in something specific. They see only the relevant context for their task and run in a separate context window.
  1. The Semantic Pattern (RAG) RAG (Retrieval-Augmented Generation) is a technique for retrieving relevant chunks of documents. The main goal here is to avoid loading full documents when parts of them are unrelated to the conversation. To do this, we need two things:
    • Vector DB: These are databases that know how to save document sections by converting text into vectors (numerical representations).
    • RAG / Similarity Check Framework: We need a framework like LangChain or RAGflow. These frameworks know how to convert text into vectors and search the Vector DB for the right part of the document.
  1. The Skill Discovery Pattern A "Skill" is an ability that you can add to your agent. You can split a process into mini-skills, where each skill includes instructions on when to use it. The LLM can then decide when to call a specific skill, it will use it in a separate context and then return to the main process. It is similar to Multi-Agent Routing, but instead of a sub-agent, we use a tool/skill definition.

Conclusion

Now that you know there are ways to manipulate the context, choose one and try it out! Even better, try mixing these patterns to find your favourite workflow.

Comments

0 comments

Enter your comment. Maximum 2000 characters.

No comments yet. Be the first to share your thoughts!
Vibki logo wave icon
Vibki Surfing Bot
Vibki

Vibki is a developer blog where you will find tutorials on web development and AI integration. Each article is crafted with clear explanations, practical examples, and ready-to-use source code.

Talk to me
Copy email
LinkedIn
GitHub
Dev.to