KDnuggets ★ 85 5 min

A Guide to Saving Token Usage with Multi-Agent AI

🔗 https://www.kdnuggets.com/a-guide-to-saving-token-usage-with-multi-agent-ai

📌 【技術指南】多代理人架構如何節省 Token 消耗?四大實作策略全解析

TL;DR:透過快取與路由策略,開發者能在擴展多代理人系統時,有效控制成本並降低延遲。

當多個 AI 代理人(Agents)為了協作完成複雜工作流而串聯在一起時,Token 的消耗量會呈爆炸式成長。從記憶體日誌(Memory logs)、詳細的工具規格說明,到系統指令(System instructions),每一項都會累積成本,並最終導致執行速度變慢與運算預算耗盡。

對於 AI 開發者而言,管理 Token 使用量已成為至關重要的課題。好消息是,透過正確實作以下四種策略,擴展多代理人架構並不代表成本會等比例增加。

🤔 避免重複讀取:使用靜態指令快取 (Static Instruction Caching)

大型語言模型(LLM)在處理連續對話時,會消耗大量的運算資源來重複閱讀相同的系統提示詞(System prompts)。

🧩 原理:預先準備「書籤」 這就像是遵守「不要重複自己」(Don’t repeat yourself)的原則。透過 Prefix-match caching(前綴匹配快取),系統可以將長篇且靜態的指令(例如「你如何扮演這個代理人」的說明手冊)儲存為鍵值對(Key-value pairs)。

當新查詢進入時,模型不需要重新閱讀整本說明書,只需開啟預先準備好的「書籤」即可直接處理新提示詞。這能顯著降低準備階段的延遲(Latency)與相關的 Token 成本。

🤔 利用語義快取:基於意圖的檢索 (Semantic Caching)

如果 AI 代理人之前已經解決過某個特定問題,為什麼還要從頭開始生成全新的回應呢?

🧩 原理:利用 Embedding 識別相似意圖 此策略利用 Embedding(將文字轉換為數值向量的技術)來保留語義特性,藉此快速識別過去處理過的相似意圖。

例如,使用者 A 問:「如何重設我的路由器?」與使用者 B 問:「重啟 Wi-Fi 設備的步驟是什麼?」,透過語義快取,系統能識別出這兩者意圖相同,進而直接提供答案,完全跳過 LLM 的運算。

🤔 按需載入工具:即時工具調用 (Just-in-Time Tooling)

在建構 AI 代理人時,一個常見的錯誤是將所有可用的 API、工具與資料庫結構(Schema)的「參考手冊」全部塞進 Context Window(上下文視窗)中。這會導致 Prompt 變得臃腫且充滿噪音,造成 Token 浪費。

🧩 原理:懶載入 (Lazy Loading) 與其提供完整的說明書,不如給予代理人一份精簡的「功能目錄」。只有當代理人判斷當下任務需要特定工具時,才會觸發去獲取該工具的詳細指令與參數。

🤔 任務升級:成本效益型的模型路由 (Model Routing)

並非所有的使用者提示詞都需要動用最強大的模型。

🧩 原理:扮演分流中心 (Triage Center) 有效的架構應該具備一個路由層(Routing layer),根據任務的性質與複雜度進行分析:

  • 輕量任務:如格式化資料、文字摘要或意圖分類,直接導向輕量化模型(甚至可以是在本地運行的免費模型)。
  • 複雜任務:如需要深度推理或跨步驟協作的任務,才「預留」給消耗 Token 較大的高階模型。

💡 實作範例:結合語義快取與模型路由

以下展示如何結合「語義快取」與「模型路由」來優化流程:

  1. 轉換向量:使用 Sentence Transformer 將查詢轉換為 Embedding。
  2. 語義檢查:計算新查詢與快取中向量的餘弦相似度(Cosine Similarity)。若相似度高於設定門檻(如 0.90),直接回傳快取結果。
  3. 任務路由
    • 若任務簡單(例如包含 “summarize” 或字數少於 100 字),導向本地免費模型(如透過 Ollama 運行的 Llama 3)。
    • 若任務複雜,則導向高階推理代理人。
  4. 更新快取:將新的向量與回應存入快取,供未來使用。

🎯 實務啟示

在建構複雜的 AI 工作流時,開發者不應僅專注於模型的能力,更應將「Token 效率」視為架構設計的核心指標。透過將靜態內容快取化、將重複意圖向量化、將工具載入延遲化,並將任務進行分流,可以在維持效能的同時,大幅降低營運成本。

🔗 來源

#AI #MultiAgent #LLM #TokenOptimization #MachineLearning #SoftwareEngineering #AIArchitecture #SemanticCaching #ModelRouting #DeveloperTips

原始資料 KDnuggets · 收集於 2026-08-04
來源原標題
A Guide to Saving Token Usage with Multi-Agent AI
作者
Iván Palomares Carrascosa
原始連結
https://www.kdnuggets.com/a-guide-to-saving-token-usage-with-multi-agent-ai

摘要原文

A Guide to Saving Token Usage with Multi-Agent AI Scaling up and streamlining a multi-agent architecture doesn't necessarily entail escalated costs if you know how to properly implement these four strategies for saving token usage. By Iván Palomares Carrascosa , KDnuggets Technical Content Specialist on August 3, 2026 in Artificial Intelligence # Introduction When multiple AI agents are strung together to cooperate and address complex workflows, the sheer volume of tokens — text elements or units, so to speak — may easily escalate. Everything adds up: from memory logs to detailed tool specifications, system instructions, and so on. Eventually, this leads to dragged down speed of executions and computing budget exhaustion. Consequently, managing token usage is vital for today's AI developers and practitioners as a whole. There's good news, though: scaling up and streamlining a multi-agent architecture doesn't necessarily entail equal scaling of costs if you know how to properly implement some strategies for saving token usage. This article introduces and shows four of them in action. # Four Key Strategies for Saving Token Usage Below are four commonly adopted best practices to streamline multi-agent AI solutions while optimizing the use of tokens. Strategies for saving token usage in multi-agent AI systems // 1. Using Static Instruction Caching (Prefix-Match Caching) Consider this one as a "don't repeat yourself" rule. Large language models (LLMs), an indispensable part of modern AI agents, invest much processing energy re-reading the same system prompts in successive turns. Prefix caching, which consists of storing key-value pairs, helps address this issue by storing such static, long instructions as a reference guide prepared ahead of time. Rather than having the model re-read the whole "how-to-act-as-this-agent" instruction manual every time, the model bookmarks a summarized state upon receiving a query. In subsequent turns, the model only needs to open that bookmark and go straight to processing the new prompt. As a result, latency due to preparation is significantly cut down, and so are the associated token costs. // 2. Using Semantic Caching: Intent-Based Recall If an AI agent has already solved a specific problem before, why ask it to generate a brand new response from scratch? This strategy leverages embeddings — numerical, vector-based representations of text that "retain" semantic properties — and uses them to quickly identify similar past intents. For instance, two distinct users' prompts like " How can I reset my router? " and " What are the steps to restart my wifi box? " would be recognized as the same intent based on semantic caching. In certain cases, this opens up the opportunity of entirely bypassing the LLM while still providing the right answer. // 3. Using Just-in-Time Tooling Also known as lazy loading, this technique is designed to tackle a frequent pitfall in AI agent building: front-loading context windows with huge "reference manuals" of every single API, tool, and database schema within their reach. Of course, that would be the perfect recipe for bloated, noisy prompts and excessive token consumption. Instead, why not give the agent a high-level, lean directory of its capabilities? Only when the agent identifies a specific task required at a given moment does it trigger the fetching of the detailed, fine-grained instructions and parameters needed for that specific tool. // 4. Using Task Escalation: Cost-Efficient Model Routing Not all user prompts require a massive, heavy-hitting model to be properly addressed. Effective multi-agent AI architectures are designed to act as triage centers, endowed with a routing layer that analyzes every incoming task based on its nature and complexity. Accordingly, simpler tasks like formatting data, summarizing text, or classifying intent are routed to lightweight, often free models capable of successfully running these tasks locally. Meanwhile, heavy, compute-intensive models where token consumption matters are "reserved" exclusively for complex tasks like those requiring deep reasoning or orchestration across multiple steps. # Implementation Example in a Nutshell Now that we've covered four practical strategies that can help optimize token usage in multi-agent AI applications, how about illustrating how some of them work through a high-level example? This code snippet illustrates how to combine two of them: semantic caching and model routing. The code uses an actual model — a sentence transformer — to convert text into the embeddings needed for semantic caching. The calls to LLMs are mocked, but you can easily replace the code (especially for the lightweight model part) with an actual, free-weights model like those available at Groq , as shown in this article , for instance. import numpy as np from sentence_transformers import SentenceTransformer # Loading a free, local model to convert text into embeddings embedder = SentenceTransformer('all-MiniLM-L6-v2') # In-memory semantic cache and similarity threshold (0.90 = 90% similar) semantic_cache = {} SIMILARITY_THRESHOLD = 0.90 def cosine_similarity(vec1, vec2): """Calculates how closely related two queries are.""" return np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2)) def route_and_respond(user_query): # 1. Converting the current query into an embedding vector query_vector = embedder.encode(user_query) # 2. Semantic Caching: Check if a similar problem was solved recently for cached_vector, past_response in semantic_cache.values(): if cosine_similarity(query_vector, cached_vector) >= SIMILARITY_THRESHOLD: return f"[Served from Cache] {past_response}" # 3. Model Routing: Triage the task based on complexity # Simple tasks get routed to a free, locally hosted model (e.g. Llama 3 via Ollama) # This routing logic is illustrative-only; not be used in production if "summarize" in user_query.lower() or len(user_query) < 100: response = call_free_local_agent(user_query) else: # Complex multi-step reasoning escalates to a larger orchestration agent response = call_heavy_reasoning_agent(user_query) # 4. Save the new vector and response to our cache for future users semantic_cache[user_query] = (query_vector, response) return response # --- Mocking Agent Functions for Illustration: no actual LLMs invoked here --- def call_free_local_agent(prompt): return "Action completed by local, zero-cost model." def call_heavy_reasoning_agent(prompt): return "Action completed by complex orchestration agent." # Example Usage: mocking the alternate use of different agents/models # Comment/uncomment to try both examples and try your own print(route_and_respond("Summarize today's server logs")) # print(route_and_respond("Draft an optimal one-month itinerary for my upcoming Japan trip. Take into consideration the set of documents, public transport timetables and other documents provided, along with real-time API information")) The complexity of the task requested in the prompt passed to route_and_respond() will determine which model type is used. The output of executing this code will be either one of the two return messages in these functions: def call_free_local_agent(prompt): return "Action completed by local, zero-cost model." def call_heavy_reasoning_agent(prompt): return "Action completed by complex orchestration agent." # Wrapping Up This article described four key strategies to be aware of when implementing multi-agent AI applications and architectures, with emphasis on optimizing token usage and reducing costs and latency. Through a practical, mock-based example, we reinforced our understanding of applying two of them in combination: semantic caching and model routing. Iván Palomares Carrascosa is a leader, writer, speaker, and adviser in AI, machine learning, deep learning & LLMs. He trains and guides others in harnessing AI in the real world. <= Previous post Next post =>

tencent/hy3:free 自動生成