ScavioScavio
产品定价文档
登录开始使用
  1. 首页
  2. 教程
  3. 如何计算每个代理搜索查询的实际成本
教程

如何计算每个代理搜索查询的实际成本

计算代理搜索查询的全部成本,包括重试、回退和 LLM 令牌。将提供商与真实数字进行比较。

获取免费API密钥API文档

搜索 API 调用的标价(Scavio 为 0.005 美元,Brave 为 5/1K 美元,Perplexity Sonar 为 5-12/1K 美元)隐藏了真实成本。代理会重试失败的查询,在结果不佳时扩展搜索,并且 LLM 本身会消耗处理搜索上下文的令牌。本教程构建了一个成本跟踪器,用于捕获代理进行的每个 API 调用,并计算每个查询的真实成本,包括重试、回退和令牌开销。

前置条件

  • 已安装 Python 3.9+
  • 请求已安装库
  • 来自 scavio.dev 的 Scavio API 密钥
  • 对 LLM 代币定价的基本了解

操作指南

步骤 1: 构建成本跟踪包装器

包装您的搜索功能以记录每个调用、其成本以及是否是重试或后备。

Python
import time
from dataclasses import dataclass, field
from typing import List

@dataclass
class SearchCall:
    provider: str
    query: str
    cost: float
    latency_ms: int
    result_count: int
    call_type: str  # 'primary', 'retry', 'fallback'
    timestamp: float = field(default_factory=time.time)

class CostTracker:
    def __init__(self):
        self.calls: List[SearchCall] = []

    def log(self, call: SearchCall):
        self.calls.append(call)

    @property
    def total_cost(self) -> float:
        return sum(c.cost for c in self.calls)

    @property
    def total_queries(self) -> int:
        return len([c for c in self.calls if c.call_type == 'primary'])

    @property
    def cost_per_query(self) -> float:
        primary = self.total_queries
        return self.total_cost / primary if primary > 0 else 0

    def summary(self) -> str:
        retries = len([c for c in self.calls if c.call_type == 'retry'])
        fallbacks = len([c for c in self.calls if c.call_type == 'fallback'])
        return (f'Queries: {self.total_queries} | Total calls: {len(self.calls)} | '
                f'Retries: {retries} | Fallbacks: {fallbacks} | '
                f'Total: ${self.total_cost:.4f} | Per query: ${self.cost_per_query:.4f}')

tracker = CostTracker()
print('Cost tracker initialized')

步骤 2: 检测您的搜索功能

为您的搜索调用添加成本跟踪。每次搜索都会记录其提供者、成本和类型(主要、重试或后备)。

Python
import requests, os

SCAVIO_KEY = os.environ['SCAVIO_API_KEY']

PROVIDER_COSTS = {
    'scavio': 0.005,
    'brave': 0.005,
    'tavily': 0.03,     # $30/1K on Researcher plan
    'perplexity': 0.005, # Sonar basic
    'google_cse': 0.005, # $5/1K paid tier
}

def tracked_search(query: str, provider: str = 'scavio',
                   call_type: str = 'primary') -> list:
    start = time.time()
    if provider == 'scavio':
        resp = requests.post('https://api.scavio.dev/api/v1/search',
            headers={'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'},
            json={'query': query, 'country_code': 'us', 'num_results': 10})
        results = resp.json().get('organic_results', [])
    else:
        results = []  # placeholder for other providers
    latency = int((time.time() - start) * 1000)
    cost = PROVIDER_COSTS.get(provider, 0.005)
    tracker.log(SearchCall(
        provider=provider, query=query, cost=cost,
        latency_ms=latency, result_count=len(results), call_type=call_type
    ))
    return results

# Simulate a typical agent session
tracked_search('best CRM tools 2026')  # primary
tracked_search('CRM pricing comparison 2026')  # primary
tracked_search('CRM pricing comparison 2026', call_type='retry')  # retry
print(tracker.summary())

步骤 3: 将 LLM 代币成本添加到计算中

搜索结果将作为上下文提供给法学硕士。计算处理搜索结果的令牌成本以获得真实的总成本。

Python
def estimate_token_cost(results: list, model: str = 'claude-sonnet') -> float:
    """Estimate LLM token cost for processing search results."""
    TOKEN_COSTS = {
        'claude-sonnet': {'input': 3.0 / 1_000_000, 'output': 15.0 / 1_000_000},
        'claude-haiku': {'input': 0.25 / 1_000_000, 'output': 1.25 / 1_000_000},
        'gpt-4o': {'input': 2.5 / 1_000_000, 'output': 10.0 / 1_000_000},
        'gpt-4o-mini': {'input': 0.15 / 1_000_000, 'output': 0.6 / 1_000_000},
    }
    costs = TOKEN_COSTS.get(model, TOKEN_COSTS['claude-sonnet'])
    # Estimate tokens: ~1.3 tokens per word
    total_words = sum(len(r.get('snippet', '').split()) + len(r.get('title', '').split())
                      for r in results)
    input_tokens = int(total_words * 1.3)
    output_tokens = 200  # typical agent response
    input_cost = input_tokens * costs['input']
    output_cost = output_tokens * costs['output']
    return round(input_cost + output_cost, 6)

# Example
results = tracked_search('python web framework comparison 2026')
llm_cost = estimate_token_cost(results)
search_cost = 0.005
total = search_cost + llm_cost
print(f'Search cost: ${search_cost:.4f}')
print(f'LLM cost:    ${llm_cost:.6f}')
print(f'Total cost:  ${total:.4f}')
print(f'Search is {search_cost/total*100:.0f}% of total per-query cost')

步骤 4: 生成成本比较报告

比较各个提供商的真实每次查询成本,考虑其典型的重试率和结果质量。

Python
def cost_report():
    providers = {
        'scavio': {'price': 0.005, 'retry_rate': 0.02, 'free_monthly': 250},
        'brave': {'price': 0.005, 'retry_rate': 0.05, 'free_monthly': 1000},
        'tavily': {'price': 0.03, 'retry_rate': 0.03, 'free_monthly': 1000},
        'perplexity_sonar': {'price': 0.005, 'retry_rate': 0.04, 'free_monthly': 0},
        'google_cse': {'price': 0.005, 'retry_rate': 0.01, 'free_monthly': 0},
    }
    monthly_queries = 5000
    print(f'Cost comparison for {monthly_queries:,} queries/month:\n')
    print(f'{"Provider":<20} {"Price/q":>8} {"Retries":>8} {"True/q":>8} {"Monthly":>10} {"After free":>10}')
    print('-' * 70)
    for name, p in providers.items():
        true_cost = p['price'] * (1 + p['retry_rate'])
        billable = max(0, monthly_queries - p['free_monthly'])
        monthly = billable * true_cost
        print(f'{name:<20} ${p["price"]:.3f}   {p["retry_rate"]*100:5.1f}%  '
              f'${true_cost:.4f}  ${monthly_queries * true_cost:>8.2f}  ${monthly:>8.2f}')

cost_report()

Python 示例

Python
import requests, os, time
from dataclasses import dataclass

SCAVIO_KEY = os.environ['SCAVIO_API_KEY']
calls = []

def search(query, call_type='primary'):
    start = time.time()
    resp = requests.post('https://api.scavio.dev/api/v1/search',
        headers={'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json'},
        json={'query': query, 'country_code': 'us', 'num_results': 10})
    results = resp.json().get('organic_results', [])
    calls.append({'cost': 0.005, 'type': call_type, 'results': len(results)})
    return results

search('CRM tools 2026')
search('CRM pricing 2026')
search('CRM pricing 2026', 'retry')
total = sum(c['cost'] for c in calls)
primary = len([c for c in calls if c['type'] == 'primary'])
print(f'Total: ${total:.3f}, Per query: ${total/primary:.4f}')

JavaScript 示例

JavaScript
const SCAVIO_KEY = process.env.SCAVIO_API_KEY;
const calls = [];

async function search(query, callType = 'primary') {
  const resp = await fetch('https://api.scavio.dev/api/v1/search', {
    method: 'POST',
    headers: { 'x-api-key': SCAVIO_KEY, 'Content-Type': 'application/json' },
    body: JSON.stringify({ query, country_code: 'us', num_results: 10 })
  });
  const results = (await resp.json()).organic_results || [];
  calls.push({ cost: 0.005, type: callType, results: results.length });
  return results;
}

await search('CRM tools 2026');
await search('CRM pricing 2026');
await search('CRM pricing 2026', 'retry');
const total = calls.reduce((s, c) => s + c.cost, 0);
const primary = calls.filter(c => c.type === 'primary').length;
console.log(`Total: $${total.toFixed(3)}, Per query: $${(total/primary).toFixed(4)}`);

预期输出

JSON
Queries: 3 | Total calls: 4 | Retries: 1 | Fallbacks: 0 | Total: $0.0200 | Per query: $0.0067

Search cost: $0.0050
LLM cost:    $0.003200
Total cost:  $0.0082
Search is 61% of total per-query cost

Cost comparison for 5,000 queries/month:

Provider             Price/q  Retries  True/q    Monthly  After free
----------------------------------------------------------------------
scavio               $0.005     2.0%  $0.0051    $25.50      $24.23
brave                $0.005     5.0%  $0.0053    $26.25      $20.98
tavily               $0.030     3.0%  $0.0309   $154.50     $123.60
perplexity_sonar     $0.005     4.0%  $0.0052    $26.00      $26.00
google_cse           $0.005     1.0%  $0.0051    $25.25      $25.25

相关教程

  • 如何为最便宜的搜索提供商构建成本路由器
  • 如何对每美元的搜索 API 质量进行基准测试
  • 如何跟踪多个代理的搜索 API 成本

常见问题

大多数开发者在15到30分钟内完成本教程。您需要一个Scavio API密钥(免费套餐即可)和可用的Python或JavaScript环境。

已安装 Python 3.9+. 请求已安装库. 来自 scavio.dev 的 Scavio API 密钥. 对 LLM 代币定价的基本了解. Scavio API密钥注册即送50个免费积分。

可以。免费套餐注册即送50个积分,完全足够完成本教程并构建一个可运行的原型解决方案。

Scavio提供原生LangChain包(langchain-scavio)、MCP服务器以及适用于任何HTTP客户端的REST API。本教程使用 the raw REST API, 但您可以根据需要适配您选择的框架。

相关资源

Best Of

Google I/O 2026 AI模式变化后最佳搜索API

Read more
Glossary

搜索 API 供应商格局(2026)

Read more
Best Of

2026 年最佳 SERP API 提供商按价格排名

Read more
Glossary

免费搜索API层级对比

Read more
Comparison

Search APIs (Scavio, Tavily, SerpAPI) vs Headless Browser (Playwright, Puppeteer, Browserbase)

Read more
Comparison

Google Places API vs SERP Local Pack API

Read more

开始构建

计算代理搜索查询的全部成本,包括重试、回退和 LLM 令牌。将提供商与真实数字进行比较。

获取免费API密钥阅读文档
ScavioScavio

面向AI智能体的实时搜索API。搜索所有平台,不仅仅是Google。

产品

  • 功能
  • 定价
  • 控制台
  • 联盟计划

开发者

  • 文档
  • API参考
  • 快速开始
  • MCP集成
  • Python SDK

替代方案

  • Tavily替代方案
  • SerpAPI替代方案
  • Firecrawl替代方案
  • Exa替代方案

工具

  • JSON格式化
  • cURL转代码
  • Token计数器
  • 全部工具

© 2026 Scavio. 保留所有权利。

Featured on TAAFT
服务条款隐私政策