

In short
Over the past 8 years, we have tested 43 different approaches to working with large language models (LLMs). Some worked, most did not. But the main takeaway stands out above all: context determines an agent’s success far more than the model itself.
Funny, isn’t it? Most teams simply waste huge budgets on expensive API calls, blaming the model for “being dumb.” Yet the root cause usually lies in memory architecture. They feed the AI garbage in the wrong format and expect gold in return.
So what is context engineering for ai agents? Simply put, it is the discipline of managing the information the model receives before it starts generating a response. Prompt engineering optimizes a single request. Context engineering manages the entire session history—ranging from 50,000 to 200,000 tokens. It is the difference between giving an employee a single instruction and handing them a full project brief with correspondence history.
| Parameter | Prompt Engineering | Context Engineering |
|---|---|---|
| Focus | Query formulation | Memory and data flow management |
| Scale | Single request | Entire interaction history |
| Optimization | Request tokens | Context window tokens |
| Tools | Prompt templates | RAG, Vector DBs, Caching |
| Result | Quality of a single response | Consistency across the entire session |
There is a technical aspect you cannot ignore. Transformers process information through an attention mechanism, and computational complexity grows quadratically with context size. It sounds complex, but the consequences are severe.
If you increase the window from 8K to 128K tokens, inference cost in a naive implementation rises 256-fold ($16^2 = 256$). This is not speculation; it is pure mathematics of the attention mechanism. You cannot simply add tokens without paying for it.
"Quadratic complexity of the attention mechanism makes long contexts exponentially more expensive." — Liu et al. Source
AI agent context window determines how much information the model holds in “RAM” simultaneously. Think of it as your computer’s RAM. GPT-4o handles 128K tokens, Claude 3.5 supports 200K, while specialized models like Llama 3 are limited to 32K.
Costs grow non-linearly. Processing 100K tokens in context requires far more resources than ten requests of 10K each. Latency increases proportionally to the square of the window size with full self-attention. Everything starts to slow down.
Commercial consequences are immediate. An agent with an unoptimized context burns through budget 3–5 times faster than competitors. When scaling to thousands of users, this difference becomes critical for unit economics. It is the line between profit and loss.
If you remember only one thing from this guide, let it be this: KV-cache hit rate is the most important metric for a production ASCN Agent. It directly impacts costs and speed. Seriously.
For example, with Claude Sonnet cached input tokens cost 0.30 USD/MTok, while uncached ones cost 3 USD/MTok — a 10-fold difference. We use special configurations to maximize efficiency, because ignoring this is literally throwing money away.
# vLLM Конфигурация для кэширования сессий
# Оптимизация hit rate KV-Cache через сохранение префиксов
from vllm import LLM, SamplingParams
# Инициализация движка LLM с управлением ID сессии
llm = LLM(
model="meta-llama/Llama-3-70b-hf",
max_model_len=8192,
# Включаем кэширование префиксов для переиспользования KV-состояний
enable_prefix_caching=True,
# Преаллоцируем память GPU под кэш, чтобы избежать фрагментации
gpu_memory_utilization=0.9
)
def generate_with_session(prompt, session_id):
"""
Гарантирует, что одинаковые префиксы кэшируются для быстрого инференса.
Избегаем добавления временных меток в системные промпты.
"""
params = SamplingParams(temperature=0.2, max_tokens=100)
outputs = llm.generate([prompt], params, request_id=session_id)
return outputs[0].outputs[0].text
Efficient systems do not dump everything into one pile. They use a three-level memory architecture. It is all about organization.
Short-term and long-term memory in AI works on the principle of the human brain. Working memory handles the current task, while the archive stores history for relevance-based search. Context management for ai agents requires clear rules for migration between levels.
Data moves from the buffer to vector storage once it reaches the token threshold. Critical decisions are duplicated in episodic memory for auditing. It is necessary to understand what happened and why.
Case: ASCN.AI FinTech Integration.
Situation: The agent lost context after 15 messages. It started getting confused.
Action: We split memory into three levels with auto-archiving.
Result: Sessions grew to 200+ messages without loss of quality. Token costs dropped by 67% (from $0.003/token to $0.001/token on a dataset of 10K requests). This is a huge win.
Here is a strange quirk of LLMs. Research by Liu et al. (2023) showed that models ignore information in the middle of long contexts. Critical data at positions 40-60% receives 40% less attention than at the beginning or end.
“Models ignore information in the middle of long contexts when extracting facts.” — Liu et al., "Lost in the Middle". Source
This phenomenon stems from the transformer architecture. The self-attention mechanism distributes weights unevenly, creating blind spots in the middle of the sequence. It is like reading a long report and skipping the middle pages.
How to deal with it? Move critical facts to the beginning and end of the context. Recursive summarization compresses the middle while preserving key entities. Dynamic prompt rewriting places real data in high-attention zones. We also use the “Recitation” technique: agents maintain a file todo.md, updating it step by step to keep global goals in focus and not lose sight of the objective. This keeps the agent sharp.
RAG for AI agents has evolved from static search to adaptive systems. It is no longer just about searching documents. Hybrid search combines vector semantics with keywords for accuracy. Re-ranking algorithms re-evaluate results before feeding them into the context.
Retrieval context optimization starts with query analysis. The system determines the type of information: facts, opinions, instructions, or examples. Each type has its own extraction strategy. You cannot process a date in the same way as a paragraph of text.
Hybrid search techniques demonstrate 35% higher accuracy compared to pure vector search (Anthropic, 2024). Combining BM25 and dense embeddings compensates for the weaknesses of each method. This is a safety net.
“Combining BM25 and dense embeddings compensates for the weaknesses of each method.” — Anthropic System Card. Source
Re-ranking algorithms filter the top 50 results down to the top 5 before inserting them into the context. The ranking model evaluates relevance considering the current state of the dialogue, removing noise and saving tokens. Less noise means better answers.
Case study: ASCN.AI Falcon Finance Drop.
Situation: The market changes every 30 seconds; static data became outdated instantly.
Action: Implemented streaming context updates with priority on fresh data.
Result (Verified): Test from June 15, 2025, Binance. Arbitrage between BTC/USDT on 3 platforms. Spread: 2.3-4.1%. Fees: 0.1% per transaction. Executed 47 transactions. Net profit: $987.
[Internal link: ASCN.AI case study on the Falcon Finance drop]
# Стратегия рекурсивного суммирования LangChain
# Сжимает историю, сохраняя ключевые сущности
from langchain.chains.summarize import load_summarize_chain
from langchain_openai import ChatOpenAI
# Инициализация модели с высокой температурой для креативности суммирования
llm = ChatOpenAI(temperature=0.3, model="gpt-4o")
def recursive_summarize(history_text, chunk_size=4000):
"""
Разбивает большой текст на чанки, суммирует каждый,
затем суммирует сами саммари для уменьшения кол-ва токенов.
"""
# Паттерн map-reduce для суммирования
chain = load_summarize_chain(llm, chain_type="map_reduce")
# В проде это срабатывает, когда история > token_limit
summary = chain.run({"input_documents": history_text})
return summary
AI agent state management determines how the system tracks task progress. The agent stores the current status, completed steps, and next actions in a structured format. It must know where it left off.
Using tools in LLMs requires dynamic API integration. Tools are loaded into the context only when needed for a specific step. This saves tokens and reduces the risk of hallucinations. However, the strategy “Mask, Don't Remove” (Mask, don’t delete) works better.
Dynamic tool removal invalidates the KV-Cache and causes schema confusion. Instead, we mask token logits during decoding to prevent the selection of specific actions based on context, keeping tool definitions in place but inactive. This is cleaner.
# Концептуальная реализация маскирования логитов
# Позволяет динамически ограничивать инструменты без удаления определений из контекста
import torch
def logits_processor(logits: torch.Tensor, active_tools: list, all_tools: list):
"""
Модифицирует вероятности вывода, форсируя выбор активных инструментов.
Индексы 'active_tools' остаются; остальные обнуляются (-infinity).
"""
mask = torch.ones_like(logits) * -float('inf')
# Маппинг имен активных инструментов на ID токенов (упрощенно)
allowed_ids = get_token_ids_for_tools(active_tools)
mask[:, allowed_ids] = 0
return logits + mask
Dynamic tool selection uses an intent classifier to predict the required tools. State persistence works through external storage rather than model context. JSON structures preserve progress between requests. The model receives only a slice of the current state. We keep everything lightweight.
Query Augmentation techniques improve the user’s original query before search. Step-back prompting generates a more general question to expand context. HyDE (Hypothetical Document Embeddings) creates hypothetical answers for vector search.
Prompt expansion adds implicit assumptions and constraints. The system finds missing parameters and formulates clarifying questions. This reduces the number of iterations and improves the first response. HyDE retrieval generates a pseudo-answer to use as a vector query.
The method shows better semantic relevance compared to the original question. Meta-prompts analyze query quality before processing, assessing completeness and ambiguity. Problematic queries are returned to the user with recommendations for improvement. Like a smart editor.
System prompt design sets the agent’s persona and constraints. Constant instructions occupy a fixed part of the context and do not change between requests. Optimizing the system prompt has a multiplicative effect on all queries.
Few-shot learning context provides examples of correct responses within the context. Quality of examples is more important than quantity. Three relevant examples work better than ten random ones. Context constraints define the boundaries of agent behavior.
A list of prohibited actions and mandatory checks reduces risks.
Best Practice: Avoid the “Few-Shot Rut.” If the context is cluttered with similar action-observation pairs from the past, the model starts mimicking patterns, even if they are suboptimal. Introduce structured variability (different templates, slight noise) to break the rhythm and prevent overgeneralization. Keep everything fresh.
Context in multi-agent systems requires synchronization between independent modules. Each agent has local memory and access to a shared knowledge pool. Agent orchestration coordinates task execution among specialized agents. A manager agent distributes subtasks and aggregates results.
Case: ASCN.AI Sales Automation.
Situation: 5 agents duplicated work and conflicted over CRM access.
Action: Implemented a shared context pool with role-based access.
Result: Lead processing speed increased 3x, data conflicts eliminated completely.
[Internal link: Business process automation]
Integration with business tools is critical for production. ASCN.AI supports connections to Gmail, Google Calendar, Slack, Telegram, Notion, and other services via API and MCP. The agent operates within existing infrastructure without manual data transfer.
For No-Code teams (ASCN.AI Platform):
Although the code examples below are for developers, ASCN.AI allows you to implement these strategies through a visual interface. You can configure memory levels and retrieval strategies using drag-and-drop nodes without writing Python. This ensures your business logic remains separate from model changes. It is accessible.
# Python Пример: Управление контекстом LangChain
# Используется разработчиками для тонкой настройки
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
max_token_limit=4000 # Жесткий лимит для предотвращения раздувания контекста
)
# Сохранение контекста
memory.save_context(
{"input": "Проанализируй эти данные по трейдам"},
{"output": "Тренд бычий, основываясь на..."}
)
RAG evaluation metrics measure the quality of retrieval and generation. Precision shows the share of relevant documents. Recall measures coverage of necessary information. Hallucination reduction is achieved by verifying facts against context sources. Unconfirmed facts are flagged or removed.
Case: Profitability during Flash Crash (October 11).
Situation: The market dropped 40% in 2 hours; standard agents provided outdated data.
Action: Switched context to streaming exchange data with priority on fresh quotes.
Result: Agents detected arbitrage opportunities with 5-40% spreads between venues, capturing instant profit during high volatility.
[Internal link: Case: Earning on Flash Crash]
Context overload in LLMs degrades response quality proportionally to the volume of noise. Extraneous information dilutes the model’s attention to critical facts. The “Less is more” rule applies to context. Always.
Anthropic 2024 research data: At 50K tokens, accuracy drops by 23%. At 100K tokens, accuracy drops by 41%. The saturation threshold for GPT-4o is ~32K for chat and 64K for analysis. Exceeding this threshold degrades performance. Auto-compression should trigger when thresholds are reached.
Structured data works better for LLMs than unstructured text. JSON and XML provide explicit schemas for parsing. Formatting JSON context requires a consistent schema. Inconsistent formatting causes extraction errors.
Semantic markup (e.g., , ) helps the model classify information. Validation before loading prevents errors. Do not make the model guess the format.
Few-shot prompting errors arise from irrelevant examples. The model extrapolates patterns. Incorrect examples break the agent’s logic. Negative examples in the context show what not to do. The contrast between correct and incorrect answers reinforces learning.
Example relevance is critical for dynamic tasks; examples older than 6 months may become outdated. Keep your examples up to date.
A common impulse is to hide agent errors (clean trace, retry, state reset). This is a mistake. Erasing failure removes evidence. Without error logs (stack traces), the model cannot adapt its internal beliefs.
Best Practice: Leave “wrong turns” in the context to reduce the likelihood of repeating the same error. Let it learn from failures.
[Forecast] According to Anthropic's roadmap, context engineering will become autonomous. Agents in 2026 will optimize their own context without engineer intervention. This will happen sooner than we think.
Multimodal context: Models process images, audio, and video within a single context. Tokenization requires new approaches. Modality synchronization requires timestamps. This is no longer just text.
Self-optimizing context: Autonomous context management allows agents to decide what to forget and what to remember. Meta-learning optimizes compression strategies. Adaptive thresholds adjust to task complexity. Context evolution tracks domain changes automatically.
Context engineering is no longer optional for production agents. Context architecture determines system cost, speed, and quality more than model selection. Teams investing in context optimization gain a 3–5x advantage in unit economics.
Start with an audit of your current architecture. Measure token consumption per request, identify memory bottlenecks, and implement context quality monitoring. Iterative improvement yields cumulative effects at scale.
The future of AI agents lies in autonomous memory management. Deploying production AI requires mature context engineering practices from day one. Do not wait.
[Internal link: Portfolio Optimization Strategies]
Disclaimer: This article contains technical recommendations and financial cases for informational purposes only. It is not financial advice or an investment recommendation. Past results of AI agents in trading (e.g., the Falcon Finance case) do not guarantee future results. Always consult a specialist before implementing financial automation systems.
Q: What is the main difference between RAG and Context Engineering?
A: RAG is a technique for retrieving documents. Context Engineering is the discipline of managing the entire context, including RAG, memory, state, and prompts. RAG is a component of Context Engineering.
Q: How to increase the context window without increasing costs?
A: Use hierarchical memory with vector search. Store the full history in external storage and load only relevant segments into the context. Apply compression to older data.
Q: What is the optimal context size for production?
A: It depends on the task. See the table below. Exceeding 50K tokens rarely pays off due to increased latency and cost.
| Task | Min. Context | Optimal | Max. ROI |
|---|---|---|---|
| Chatbot | 2K | 4K | 8K |
| Analysis | 8K | 32K | 64K |
| Code migration | 16K | 64K | 128K |
Q: How to handle hallucinations caused by poor context?
A: Implement fact verification against context sources. Use structured data instead of unstructured text. Add negative examples.
Q: Can one context be used for multiple agents?
A: Yes, via a shared memory pool with role-based access. Each agent sees only the relevant part. Synchronization prevents write conflicts.
Q: Is “Mask, Don't Remove” better than dynamic tool loading?
A: Yes. Removing tools breaks the KV-Cache and confuses the model regarding past actions. Logit masking preserves history stability.
Q: How does ASCN.AI handle No-Code implementation?
A: ASCN.AI abstracts the complexity of LangChain/LlamaIndex into visual nodes. You configure the “Memory Level” and “Search Strategy” through the interface, while the platform handles tokenization and API calls.
Q: What is the “Lost in the Middle” problem?
A: Models pay less attention to information in the middle of long texts (positions 40–60%). Mitigation: place key facts at the beginning/end or use recursive summarization.