Track how often your brand surfaces in answers from Perplexity, Gemini, ChatGPT, and Claude with mention rate, position, sentiment, and competitor data.
You are an Answer Engine Optimization (AEO) analyst. You measure brand visibility across AI search engines and tell the user how to improve it. WORKFLOW: 1. Call query_ai_engine for each (provider, query) pair the user wants tracked. 2. For each raw response, call detect_mention to extract mention rate, position, sentiment, competitors mentioned, and citations. 3. Aggregate results with build_report into a structured visibility scorecard. 4. If a competitor overtakes the user's brand on a tracked query, call send_alert. RULES: - Always run queries through Cloudflare AI Gateway, never the provider directly. - Cache by (provider, query) for 24h to control cost. - Report mention rate as a percentage rounded to one decimal. - When sentiment is mixed, classify as "neutral" rather than guessing. - Never claim a brand is "winning" without comparing against at least 3 competitors.
import { agent, tool } from "@agent-sdk"
import { z } from "zod"
const queryEngineInput = z.object({
provider: z.enum(["perplexity", "gemini", "deepseek", "openai", "anthropic", "grok"]),
query: z.string().min(5).max(300),
})
const detectMentionInput = z.object({
raw: z.string(),
brand: z.string(),
competitors: z.array(z.string()).default([]),
})
export default agent({
model: "claude-sonnet-4-6",
permissionMode: "bypassPermissions",
maxTurns: 30,
systemPrompt: `...`, // see System Prompt section above
tools: {
query_ai_engine: tool({
description: "Run a query against an AI search engine via CF AI Gateway",
inputSchema: queryEngineInput,
execute: async ({ provider, query }) => {
const url = `https://gateway.ai.cloudflare.com/v1/${process.env.CF_ACCOUNT_ID}/${process.env.CF_GATEWAY_ID}/${provider}/chat/completions`
// POST { model, messages: [{ role: "user", content: query }] }
},
}),
detect_mention: tool({
description: "Extract brand mention metadata from a raw model response",
inputSchema: detectMentionInput,
execute: async ({ raw, brand, competitors }) => {
// returns { mentioned, position, sentiment, competitorsMentioned, citations }
},
}),
build_report: tool({
description: "Aggregate detections into a visibility scorecard",
inputSchema: z.object({ runs: z.array(z.any()) }),
execute: async ({ runs }) => {
// returns { mentionRate, avgPosition, sentimentBreakdown, competitorMatrix }
},
}),
send_alert: tool({
description: "Send alert when a competitor overtakes the brand",
inputSchema: z.object({ text: z.string() }),
execute: async ({ text }) => {
// POST to ALERT_WEBHOOK_URL
},
}),
},
})CF_ACCOUNT_IDCloudflare account IDCF_GATEWAY_IDCloudflare AI Gateway slugPERPLEXITY_API_KEYPerplexity API key (sonar models)GOOGLE_GENAI_KEYGoogle AI Studio key for GeminiAI search now drives 30% of brand discovery and nobody is measuring it. AEO tracking is the new SEO console — first mover advantage is real.