如果结果不受控制,调用搜索 API 的代理每次查询可能会消耗数千个令牌。令牌预算系统可确保搜索结果永远不会超过定义的限制,从而保持代理的上下文窗口可用于推理。这对于每轮调用多个工具的代理来说尤其重要。
前置条件
- Python 3.8+
- 安装 tiktoken (pip install tiktoken)
- Scavio API 密钥
操作指南
步骤 1: 安装 tiktoken 进行代币计数
设置令牌计数来衡量搜索结果的大小。
Bash
pip install tiktoken步骤 2: 构建预算搜索功能
创建一个尊重代币预算的搜索功能。
Python
import requests, os, tiktoken
H = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}
enc = tiktoken.encoding_for_model('gpt-4')
def budgeted_search(query: str, platform: str = 'google', max_tokens: int = 300) -> str:
resp = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'platform': platform, 'query': query}, timeout=10)
results = resp.json().get('organic', [])
output_lines = []
token_count = 0
for r in results:
line = f"{r.get('title','')}: {r.get('snippet','')}"
line_tokens = len(enc.encode(line))
if token_count + line_tokens > max_tokens:
break
output_lines.append(line)
token_count += line_tokens
return '\n'.join(output_lines)步骤 3: 创建每日预算跟踪器
跟踪所有搜索调用的总令牌和信用使用情况。
Python
from dataclasses import dataclass, field
from datetime import date
@dataclass
class SearchBudget:
daily_credit_limit: int = 100
daily_token_limit: int = 50000
credits_used: int = 0
tokens_used: int = 0
date: str = field(default_factory=lambda: date.today().isoformat())
def can_search(self) -> bool:
if self.date != date.today().isoformat():
self.reset()
return self.credits_used < self.daily_credit_limit
def record(self, tokens: int):
self.credits_used += 1
self.tokens_used += tokens
def reset(self):
self.credits_used = 0
self.tokens_used = 0
self.date = date.today().isoformat()
def remaining(self) -> dict:
return {'credits': self.daily_credit_limit - self.credits_used,
'tokens': self.daily_token_limit - self.tokens_used}
budget = SearchBudget(daily_credit_limit=50)
def search_with_budget(query: str, platform: str = 'google', max_tokens: int = 300) -> str:
if not budget.can_search():
return '[Budget exceeded - no more searches today]'
result = budgeted_search(query, platform, max_tokens)
budget.record(len(enc.encode(result)))
return result步骤 4: 与代理框架集成
将预算搜索连接到代理的工具系统中。
Python
# Example with a simple agent loop:
def agent_research(question: str, budget: SearchBudget) -> str:
# Adaptive token budget based on remaining allowance
remaining = budget.remaining()
per_search_budget = min(300, remaining['tokens'] // 3) # Reserve for 3 searches
# Search with budget
google_ctx = search_with_budget(question, 'google', per_search_budget)
reddit_ctx = search_with_budget(question, 'reddit', per_search_budget)
context = f"Google:\n{google_ctx}\n\nReddit:\n{reddit_ctx}"
total_tokens = len(enc.encode(context))
print(f"Research used {budget.credits_used} credits, {total_tokens} tokens")
print(f"Remaining: {budget.remaining()}")
return contextPython 示例
Python
import requests, os, tiktoken
H = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}
enc = tiktoken.encoding_for_model('gpt-4')
def budgeted_search(query, max_tokens=300):
r = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'platform': 'google', 'query': query}).json()
lines, tokens = [], 0
for x in r.get('organic',[]):
line = f"{x['title']}: {x.get('snippet','')}"
t = len(enc.encode(line))
if tokens + t > max_tokens: break
lines.append(line); tokens += t
return '\n'.join(lines)JavaScript 示例
JavaScript
// Token counting in JS (approximate, using char/4 heuristic):
async function budgetedSearch(query, maxTokens = 300) {
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'},
body: JSON.stringify({platform: 'google', query})
});
const results = (await r.json()).organic || [];
let lines = [], tokens = 0;
for (const x of results) {
const line = `${x.title}: ${x.snippet || ''}`;
const t = Math.ceil(line.length / 4);
if (tokens + t > maxTokens) break;
lines.push(line); tokens += t;
}
return lines.join('\n');
}预期输出
JSON
A token budget system that controls how many tokens search results consume in your agent's context window, with daily credit tracking.