Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

feat(exa): return cited and structured research answers

Rudy Celekli0a12636eba91
diff --git a/tests/clis/exa-answer.test.cjs b/tests/clis/exa-answer.test.cjs
new file mode 100644
index 0000000..88fdafe
--- /dev/null
+++ b/tests/clis/exa-answer.test.cjs
@@ -0,0 +1,36 @@
+const {test} = require('node:test')
+const assert = require('node:assert/strict')
+const {spawnSync} = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/exa.js')
+function run(args, network = true) {
+ const script = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected network request');
+ return {status:200, text:async()=>JSON.stringify({url,method:options.method,request:JSON.parse(options.body),answer:'Fixture answer',citations:[{url:'https://example.org/source'}]})};
+ }; process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath,['-e',script],{encoding:'utf8',timeout:5000,env:{PATH:process.env.PATH,EXA_API_KEY:'fixture-secret'}})
+}
+function output(r) { assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout) }
+test('cited answers use the documented endpoint and preserve sources',()=>{
+ const result=output(run(['answer','What','changed?']))
+ assert.equal(result.url,'https://api.exa.ai/answer');assert.equal(result.method,'POST')
+ assert.deepEqual(result.request,{query:'What changed?'});assert.equal(result.answer,'Fixture answer')
+ assert.deepEqual(result.citations,[{url:'https://example.org/source'}])
+})
+test('answer supports structured output and source text without streaming',()=>{
+ const schema={type:'object',properties:{summary:{type:'string'}}}
+ const result=output(run(['answer','--query','Research','--model','exa-pro','--system-prompt','Use dated sources','--text','false','--output-schema',JSON.stringify(schema)]))
+ assert.deepEqual(result.request,{query:'Research',model:'exa-pro',systemPrompt:'Use dated sources',text:false,outputSchema:schema})
+ assert.equal(Object.hasOwn(result.request,'stream'),false)
+ assert.equal(output(run(['answer','Research','--text'])).request.text,true)
+})
+test('invalid query, booleans and schemas fail before a billed request',()=>{
+ for(const args of [['answer'],['answer','--query'],['answer','Q','--text','maybe'],['answer','Q','--output-schema','null'],['answer','Q','--output-schema','[]'],['answer','Q','--output-schema','{'],['answer','Q','--model']]) {
+ const result=run(args,false);assert.equal(result.status,1);assert.doesNotMatch(result.stderr,/Unexpected network/)
+ }
+})
+test('answer dry run shows the request without disclosing auth',()=>{
+ const result=output(run(['answer','Research','--dry-run'],false))
+ assert.equal(result.url,'https://api.exa.ai/answer');assert.equal(result.body.query,'Research');assert.equal(result._dry_run,true)
+ assert.equal(JSON.stringify(result).includes('fixture-secret'),false)
+})
diff --git a/tools/clis/exa.js b/tools/clis/exa.js
index 87b7629..ceb9cb8 100755
--- a/tools/clis/exa.js
+++ b/tools/clis/exa.js
@@ -77,6 +77,30 @@ async function main() {
let result
switch (cmd) {
+ case 'answer': {
+ const query = args.query ?? rest.join(' ')
+ if (typeof query !== 'string' || !query.trim()) throw new Error('answer requires a nonempty --query or positional question')
+ const body = { query }
+ for (const [flag, field] of [['model', 'model'], ['system-prompt', 'systemPrompt']]) {
+ if (args[flag] === undefined) continue
+ if (typeof args[flag] !== 'string' || !args[flag].trim()) throw new Error(`--${flag} requires a value`)
+ body[field] = args[flag]
+ }
+ if (args.text !== undefined) {
+ if (args.text === true || args.text === 'true') body.text = true
+ else if (args.text === 'false') body.text = false
+ else throw new Error('--text must be true or false (or a bare flag for true)')
+ }
+ if (args['output-schema'] !== undefined) {
+ let schema
+ try { schema = JSON.parse(args['output-schema']) } catch { throw new Error('--output-schema must be a JSON object') }
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema)) throw new Error('--output-schema must be a JSON object')
+ body.outputSchema = schema
+ }
+ result = await api('POST', '/answer', body)
+ break
+ }
+
case 'search': {
const query = args.query || rest.join(' ')
if (!query) { result = { error: '--query required' }; break }
@@ -131,6 +155,7 @@ async function main() {
result = {
error: 'Unknown command',
usage: {
+ answer: 'answer [question | --query <q>] [--model <model>] [--system-prompt <instructions>] [--text true|false] [--output-schema <JSON object>]',
search: 'search --query <q> [--type neural|fast|auto|deep-lite|deep|deep-reasoning|instant] [--num <n>] [--category company|research paper|news|personal site|financial report|people] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--include-text <phrases>] [--exclude-text <phrases>] [--start-published <ISO>] [--end-published <ISO>] [--user-location <CC>] [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
'find-similar': 'find-similar --url <url> [--num <n>] [--include-domains <d1,d2>] [--exclude-domains <d1,d2>] [--start-published <ISO>] [--end-published <ISO>] [--text] [--highlights] [--summary]',
contents: 'contents --urls <url1,url2> [--text] [--highlights] [--summary] [--max-chars <n>] [--highlight-query <q>] [--summary-query <q>]',
diff --git a/tools/integrations/exa.md b/tools/integrations/exa.md
index a17816d..8dd1fd4 100644
--- a/tools/integrations/exa.md
+++ b/tools/integrations/exa.md
@@ -143,3 +143,16 @@ All three can be requested in the same call:
- cold-email
- lead-magnets
- marketing-ideas
+
+### Answers with source citations
+
+Use `answer` when you need a provider-generated answer alongside the web sources it used. The CLI returns the complete JSON response, preserving `citations`, `requestId` and provider cost metadata instead of flattening the answer into unsourced text. The output remains model-generated; check cited pages before using its claims.
+
+```bash
+node tools/clis/exa.js answer --query "What changed in our market this quarter?" --model exa-pro
+node tools/clis/exa.js answer "Research the category" --output-schema '{"type":"object","properties":{"summary":{"type":"string"}}}' --dry-run
+```
+
+`--system-prompt` supplies additional generation instructions. `--text` (or `--text true`) requests full source text; `--text false` explicitly disables it. `--output-schema` accepts a JSON object, forwarded as `outputSchema`; schema validity beyond this shape is provider-validated. Answers use one non-streaming POST to `/answer`, may consume credits and are never automatically retried. Omitted options retain provider defaults. Model availability depends on the account/API.
+
+See [Exa’s answer contract](https://exa.ai/docs/reference/answer). Contract tests use fake credentials and local response fixtures; they do not establish authenticated provider acceptance or live answer quality.