ScavioScavio
产品定价文档
登录开始使用
  1. 首页
  2. 教程
  3. 如何跟踪 Perplexity Sonar API 积分使用情况
教程

如何跟踪 Perplexity Sonar API 积分使用情况

监控 Perplexity Sonar API 积分使用情况并在积分耗尽前设置警报。防止意外信用抹除。

获取免费API密钥API文档

一位 r/Perplexity 用户报告 Sonar API 积分在没有警告的情况下被擦除。本教程构建了一个简单的监控层来跟踪使用情况并在耗尽之前发出警报。

前置条件

  • 困惑 API 密钥
  • Python 3.8+
  • 通知渠道(Slack、电子邮件或短信)

操作指南

步骤 1: 记录每个 API 调用

使用使用日志记录 Sonar API 调用。

Python
import requests, json, datetime

def log_usage(model, tokens_in, tokens_out, cost):
    with open('sonar_usage.jsonl', 'a') as f:
        f.write(json.dumps({
            'ts': datetime.datetime.now().isoformat(),
            'model': model, 'tokens_in': tokens_in,
            'tokens_out': tokens_out, 'cost': cost
        }) + '\n')

步骤 2: 计算运行成本

Sonar 定价:$1/M 代币输入/输出 + $5/1K 请求。

Python
def running_cost(log_file='sonar_usage.jsonl'):
    total = 0
    requests_count = 0
    with open(log_file) as f:
        for line in f:
            entry = json.loads(line)
            total += (entry['tokens_in'] + entry['tokens_out']) / 1e6
            requests_count += 1
    request_cost = (requests_count / 1000) * 5
    return total + request_cost

步骤 3: 设置预算提醒

当使用量接近您的预算时发出警报。

Python
def check_budget(budget=50):
    cost = running_cost()
    if cost > budget * 0.8:
        send_alert(f'Sonar API at {cost:.2f}/{budget} ({cost/budget*100:.0f}%)')
    if cost > budget:
        send_alert(f'OVER BUDGET: Sonar API at {cost:.2f}/{budget}')

步骤 4: 构建迁移触发器

超出预算时自动切换到备份 API。

Python
def search(query, budget_exceeded=False):
    if budget_exceeded:
        # Failover to Scavio
        return requests.post('https://api.scavio.dev/api/v1/search',
            headers={'x-api-key': os.environ['SCAVIO_API_KEY']},
            json={'platform': 'google', 'query': query}).json()
    return sonar_search(query)

Python 示例

Python
# Track Sonar usage, alert at 80% budget, failover to Scavio at 100%.
# Prevents surprise credit exhaustion.

JavaScript 示例

JavaScript
// Same logging and alert pattern in JS/TS.

预期输出

JSON
JSONL usage log, budget alerts at 80% and 100%, automatic failover to Scavio when Sonar budget is exceeded.

相关教程

  • 如何构建供应商弹性搜索堆栈 (2026)
  • 如何用 Scavio 替换 Tavily(迁移指南)

常见问题

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

困惑 API 密钥. Python 3.8+. 通知渠道(Slack、电子邮件或短信). Scavio API密钥注册即送50个免费积分。

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

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

相关资源

Best Of

2026 年最佳困惑声纳替代方案

Read more
Best Of

2026年最佳答案引擎API

Read more
Solution

Sonar API

Read more
Glossary

Sonar API

Read more
Comparison

Tavily vs Perplexity API (Sonar)

Read more
Comparison

Scavio vs Perplexity Sonar

Read more

开始构建

监控 Perplexity Sonar API 积分使用情况并在积分耗尽前设置警报。防止意外信用抹除。

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

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

产品

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

开发者

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

替代方案

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

工具

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

© 2026 Scavio. 保留所有权利。

Featured on TAAFT
服务条款隐私政策