Skip to content

Add brave-answer skill: synthesized cited answers via Brave Answers API - #31

Open
hui-zheng wants to merge 1 commit into
badlogic:mainfrom
hui-zheng:add-brave-answer-skill
Open

Add brave-answer skill: synthesized cited answers via Brave Answers API#31
hui-zheng wants to merge 1 commit into
badlogic:mainfrom
hui-zheng:add-brave-answer-skill

Conversation

@hui-zheng

@hui-zheng hui-zheng commented May 3, 2026

Copy link
Copy Markdown

Summary

Adds brave-answer, wrapping the Brave Answers API for one-shot synthesized answers with numbered citations. Companion to brave-search.

Closes #30

Files

  • brave-answer/SKILL.md — frontmatter, setup, usage, common mistakes
  • brave-answer/answer.js — streaming SSE client, citation parser, numbered references output
  • brave-answer/package.json"type": "module", no runtime dependencies
  • README.md — skill row, requirements line, Claude Code symlink lines (user + project)

API specifics

  • POST https://api.search.brave.com/res/v1/chat/completions
  • Auth via x-subscription-token header
  • Always streams (citations require stream: true per docs)
  • Parses <citation>{json}</citation>[N] markers + a --- Sources --- block
  • Strips <enum_item> and <usage> tags silently

Flags

  • --research — multi-search deep mode (slow, can take minutes)
  • --country <code> / --language <code> — locale (defaults: US / en, matching brave-search convention)
  • --raw — debug; prints raw API content with tags

Conscious deviations from brave-search

A couple of intentional small departures, flagged so they don't read as oversights:

  • Frontmatter description is longer than brave-search's one-liner — leans into trigger keywords ("what is X", "summarize Y", current-events Q&A) so the skill router picks it over brave-search when the user wants a synthesized answer. Happy to tighten if you'd prefer a shorter description.
  • SKILL.md has a Common Mistakes section that brave-search doesn't — covers the BRAVE_ANSWER_API_KEY-vs-BRAVE_API_KEY trap and --research runtime gotcha, both of which agents tripped on during testing.

Code style, arg parsing, error handling, install ritual, country defaults, env-var pattern, README integration all mirror brave-search.

Sample output

WebGPU is a modern, low-level web API that enables high-performance graphics
rendering and general-purpose GPU computing in browsers...[1][3][7]

--- Sources ---
[1] https://gpuweb.github.io/gpuweb/explainer/
    WebGPU is a proposed Web API to enable webpages to use the system's GPU...
[3] https://multilogin.com/glosssary/webgpu-browser/
    WebGPU is a modern graphics API designed to provide high-performance...
[7] https://threejsroadmap.com/blog/webgl-vs-webgpu-explained
    WebGPU is a next-generation graphics API designed from the ground up...

Test plan

  • Smoke-tested with real API key against several questions (locale, --raw, missing-arg-value error path)
  • Reviewer pass: SSE chunk-boundary parsing safe (content fully accumulated before regex), ESM module type explicit, country/language defaults consistent between code and docs

🤖 Generated with Claude Code

@hui-zheng
hui-zheng marked this pull request as ready for review May 5, 2026 01:27
@hui-zheng
hui-zheng force-pushed the add-brave-answer-skill branch from 46365d8 to 7ea2ad5 Compare May 5, 2026 01:28
@hui-zheng

Copy link
Copy Markdown
Author

Out of draft. Changes since the initial draft (force-pushed as a single squashed commit):

  • Setup wording corrected — verified against Brave's pricing page. The plan is "Answers" with $5/mo free credits, not a literally-named "Free Answers subscription". SKILL.md and the subscribe step updated.
  • --country / --language defaults now match the docsanswer.js was sending null (API default) while the docs claimed US / en. Now defaults to US/en and always passes them, matching brave-search convention. Country uppercased like search.js.
  • package.json "author" field added for parity with brave-search.
  • Common Mistakes section + verbose frontmatter description kept — flagged as conscious deviations in the PR description above. Happy to tighten either if you'd prefer one-liner consistency with the rest of the skills.

Live smoke test ran clean against the real Answers endpoint.

Companion to brave-search: returns one prose answer with API-vouched
citations in a single round-trip, instead of a list of results to stitch
together by hand. Wraps POST /res/v1/chat/completions with stream:true
and enable_citations:true, parses streamed <citation> tags into a
numbered references block.

Research mode (--research) is multi-search deep mode: significantly
slower and more expensive, and the API does not return per-claim
citations in this mode (the answer is wrapped in <answer>{...}</answer>
JSON instead). The parser handles both shapes; enable_citations is
omitted in research mode (the API rejects it as a 422).

Requires the Brave Search API "Answers" plan ($5/mo free credits) and
BRAVE_ANSWER_API_KEY env var. Native fetch only, no runtime npm deps;
package.json + lockfile mirror brave-search install ritual for
consistency.

Code style mirrors brave-search/search.js: indexOf+splice arg parsing,
factored fetchBraveAnswer() and parseAnswer(), top-level try/catch
with `Error: ...` formatting, country=US / language=en defaults.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@hui-zheng
hui-zheng force-pushed the add-brave-answer-skill branch from 7ea2ad5 to f6e33c0 Compare May 5, 2026 02:18
@hui-zheng

Copy link
Copy Markdown
Author

Caught a real bug while smoke-testing --research end-to-end after the cleanup pass. Force-pushed an amended commit:

  • enable_citations: true + enable_research: true is rejected by the API (HTTP 422: "Research mode doesn't support 'enable_citations' option"). answer.js was always sending both. Now omits enable_citations when --research is set.
  • Research mode emits a different output shape: synthesized answer wrapped as <answer>{"answer": "..."}</answer> JSON, no per-claim <citation> tags, plus <queries> / <analyzing> / <thinking> / <progress> / <blindspots> planning metadata. Renamed parseCitations()parseAnswer() and taught it both shapes; metadata tags stripped in either mode.
  • SKILL.md --research description updated to flag (1) cost — a single research call ran ~$0.60 in smoke testing, well past the $5/mo free credits on a few uses, and (2) no citations in research mode.

Tested both modes against the live API:

Force-push is one squashed commit on top of upstream/main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: add brave-answer skill (Brave Answers API for sourced Q&A)

1 participant