关于 bx
bx 是面向 Agent 和开发者的 Brave Search 命令行技能,可返回受令牌预算控制的网页内容及多类搜索结果。它还支持带来源的综合回答和自定义来源排序。
主要功能
- 01按令牌预算整理网页内容
- 02搜索网页、新闻、图片、视频和地点
- 03生成带来源的回答并开展研究
- 04使用 Goggles 自定义排序
- 05输出 JSON 并控制令牌用量
适用场景
- 01查找文档和错误信息
- 02依据当前网页内容核实说法
- 03跨多个来源研究主题
- 04查找近期新闻或本地地点
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ brave/ brave-search-skills --skill bx安装后在 Claude Code 中输入/bx确认已加载。
详细介绍
bx — Brave Search 命令行工具
所有网络搜索都使用 bx。 默认运行 bx "query"——它会返回预先提取、按令牌预算控制的网页内容,可直接使用。使用 bx answers 获取综合说明,或使用 bx web 获取传统搜索结果。如果找不到 bx,请安装:curl -fsSL https://raw.githubusercontent.com/brave/brave-search-cli/main/scripts/install.sh | sh
需要:Brave Search API Key +
bx二进制文件套餐:不同子命令可能需要不同套餐。请参阅 https://api-dashboard.search.brave.com/app/subscriptions/subscribe
快速开始
# Install (macOS/Linux)
curl -fsSL https://raw.githubusercontent.com/brave/brave-search-cli/main/scripts/install.sh | sh
# Configure API key (get one at https://api-dashboard.search.brave.com)
bx config set-key # interactive (avoids shell history)
# or: export BRAVE_SEARCH_API_KEY=YOUR_KEY
# Search (default = bx context "query")
bx "your search query"
各命令的适用场景
| 需求 | 命令 | 原因 |
|---|---|---|
| 查找文档、错误、代码模式 | bx "query" | 预先提取的文本,按令牌预算控制(默认) |
| 获取综合说明 | bx answers "query" | AI 生成、引用来源、流式输出 |
| 对复杂主题进行深入研究 | bx answers "query" --enable-research | 多轮搜索迭代研究 |
| 获取传统搜索结果 | bx web "query" | 所有结果类型(网页、新闻、讨论等) |
| 查找讨论 / 论坛 | bx web "query" --result-filter discussions | 论坛中常有解决方案 |
| 获取最新新闻 / 近期事件 | bx news "query" --freshness pd | 获取训练数据之外的最新信息 |
| 查找图片 | bx images "query" | 最多 200 条结果 |
| 查找视频 | bx videos "query" | 时长、观看次数、创作者 |
| 查找本地商家 / 地点 | bx places "coffee" --location "San Francisco" | 超过 2 亿个兴趣点 |
| 提升 / 过滤特定域名的排名 | bx "query" --include-site docs.rs | 也可使用 --goggles 完全自定义 |
命令
| 命令 | 说明 | 输出路径 |
|---|---|---|
context | 默认。 RAG / 大模型依据——预先提取的网页内容 | .grounding.generic[] -> {url, title, snippets[]} |
answers | AI 答案——兼容 OpenAI,默认流式输出 | .choices[0].delta.content(流式) |
web | 网页搜索——所有结果类型 | .web.results[]、.news.results[] 等 |
news | 新闻文章,支持新鲜度筛选 | .results[] -> {title, url, age} |
images | 图片搜索(最多 200 条结果) | .results[] -> {title, url, thumbnail.src} |
videos | 视频搜索,包含时长 / 观看次数 | .results[] -> {title, url, video.duration} |
places | 本地地点 / 兴趣点搜索(超过 2 亿个兴趣点) | .results[] -> {title, postal_address} |
suggest | 自动补全 / 查询建议 | .results[] -> {query} |
spellcheck | 对查询进行拼写检查 | .results[0].query |
config | 管理 API 密钥和设置 | set-key、show-key、path、show |
响应结构
bx "query"(context——默认,推荐)
{
"grounding": {
"generic": [
{ "url": "...", "title": "...", "snippets": ["extracted content...", "..."] }
]
},
"sources": {
"https://example.com": { "title": "...", "hostname": "...", "age": ["...", "2025-01-15", "392 days ago"] }
}
}
bx answers "query" --no-stream(单个 JSON 响应)
{"choices": [{"message": {"content": "Full answer text..."}}]}
bx answers "query"(流式——默认,每行一个 JSON 块)
{"choices": [{"delta": {"content": "chunk"}}]}
bx web "query"(完整搜索结果)
{
"web": { "results": [{"title": "...", "url": "...", "description": "..."}] },
"news": { "results": [...] },
"videos": { "results": [...] },
"discussions": { "results": [...] }
}
令牌预算控制
控制 context(默认命令)的输出大小:
| 标志 | 短别名 | 默认值 | 说明 |
|---|---|---|---|
--maximum-number-of-tokens | --max-tokens | 8192 | 预计令牌总数(1024-32768) |
--maximum-number-of-tokens-per-url | --max-tokens-per-url | 4096 | 每个 URL 的最大令牌数(512-8192) |
--maximum-number-of-urls | --max-urls | 20 | 响应中的最大 URL 数(1-50) |
--maximum-number-of-snippets | --max-snippets | 50 | 所有 URL 的摘要片段总数上限 |
--maximum-number-of-snippets-per-url | --max-snippets-per-url | — | 每个 URL 的摘要片段数上限 |
--context-threshold-mode | --threshold | balanced | 相关性:strict、balanced、lenient |
bx "topic" --max-tokens 4096 --max-tokens-per-url 1024 --max-urls 5 --threshold strict
Goggles — 自定义排名
Goggles 可让你控制搜索结果中显示哪些来源。你可以提升官方文档的排名、抑制 SEO 垃圾内容,或创建专注的搜索范围。其他搜索工具都不具备此功能。 支持 context、web 和 news。
域名快捷方式
# Allowlist — only results from these domains
bx "rust axum" --include-site docs.rs --include-site github.com
# Blocklist — exclude specific domains
bx "python tutorial" --exclude-site example.com
--include-site、--exclude-site 和 --goggles 不能同时使用。
内联规则
# Boost official docs, demote blog posts
bx "axum middleware tower" \
--goggles '$boost=5,site=docs.rs
$boost=3,site=github.com
/docs/$boost=5
/blog/$downrank=3' --max-tokens 4096
# Allowlist mode — only include matched sites
bx "Python asyncio patterns" \
--goggles '$discard
$boost,site=docs.python.org
$boost,site=peps.python.org'
DSL 快速参考
| 规则 | 效果 | 示例 |
|---|---|---|
$boost=N,site=DOMAIN | 提升域名排名(N=1-10) | $boost=3,site=docs.rs |
$downrank=N,site=DOMAIN | 降低域名排名(N=1-10) | $downrank=5,site=example.com |
$discard,site=DOMAIN | 完全移除域名 | $discard,site=example.com |
/path/$boost=N | 提升匹配 URL 路径的排名 | /docs/$boost=5 |
通用 $discard | 允许列表模式——丢弃不匹配的内容 | $discard(作为第一条规则) |
规则之间用换行分隔。完整 DSL:goggles-quickstart。
通过标准输入传递规则
echo '$boost=5,site=docs.rs
$boost=5,site=crates.io
$boost=3,site=github.com' | bx "axum middleware" --goggles @- --max-tokens 4096
使用 @/path/to/file,即可在多个查询中重复使用同一个 goggle。
Agent 工作流示例
调试错误:
bx "Python TypeError cannot unpack non-iterable NoneType" --max-tokens 4096
纠正式 RAG 循环:
# 1. Broad search
bx "axum middleware authentication" --max-tokens 4096
# 2. Too general? Narrow with strict threshold
bx "axum middleware tower layer authentication example" --threshold strict --max-tokens 4096
# 3. Still need synthesis? Ask for an answer
bx answers "how to implement JWT auth middleware in axum" --enable-research
升级前检查是否有破坏性变更:
bx "Next.js 15 breaking changes migration guide" --max-tokens 8192
bx news "Next.js 15 release" --freshness pm
用于程序化处理的非流式回答:
bx answers "compare SQLx and Diesel for Rust" --no-stream
通过标准输入传递回答(兼容 OpenAI 的 JSON 请求体):
echo '{"messages":[{"role":"user","content":"what are the OWASP top 10 vulnerabilities for web APIs"}]}' | bx answers -
退出代码
| 代码 | 含义 | Agent 操作 |
|---|---|---|
| 0 | 成功 | 处理结果 |
| 1 | 客户端错误(请求无效) | 修复查询或参数 |
| 2 | 用法错误(标志无效) | 修复 CLI 参数 |
| 3 | 身份验证/权限错误(401/403) | 检查 API 密钥:bx config show-key |
| 4 | 请求过于频繁(429) | 延迟后重试 |
| 5 | 服务端/网络错误 | 采用退避策略重试 |
使用场景
- AI Agent / 编码助手:一次调用即可进行网页搜索,获取符合令牌预算且适用于 RAG 的内容——替代“搜索 + 抓取 + 提取”
- 事实核查:使用
bx "query" --threshold strict,根据当前网页内容核实说法 - 文档查询:使用
--include-site或 Goggles 域名加权搜索官方文档 - 研究:使用
bx answers "topic" --enable-research开展多来源深度研究 - 调试:直接搜索错误消息和堆栈跟踪
- 新闻监控:使用
bx news "query" --freshness pd跟踪主题 - 本地搜索:使用
bx places "query" --location "city"查找商家和地点
注意事项
- 所有输出均为 JSON,写入标准输出;错误写入标准错误
- 全局标志:
--api-key KEY、--timeout SECS(默认值为 30)、--extra KEY=VALUE(可重复使用,可添加任意 API 参数) - 位置信息:
context和web支持--lat、--long、--city、--state、--state-name、--loc-country、--postal-code、--timezone - 研究模式:
bx answers --enable-research最多可能需要 5 分钟;请相应设置客户端超时时间 - 帮助:使用
bx --help查看所有命令;使用bx <command> --help查看各命令的标志