add-ai-endpoint
Scaffold a Claude API powered endpoint with system prompt, structured output, token tracking, and rate limiting. Use when adding AI features like chatbot, matching, or text generation.
Install
mkdir -p .claude/skills/add-ai-endpoint && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/16707" && unzip -o skill.zip -d .claude/skills/add-ai-endpoint && rm skill.zipInstalls to .claude/skills/add-ai-endpoint
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Scaffold a Claude API powered endpoint with system prompt, structured output, token tracking, and rate limiting. Use when adding AI features like chatbot, matching, or text generation.About this skill
add-ai-endpoint
Scaffolds a new AI-powered API endpoint backed by the Anthropic Claude API. Handles system prompt design, structured output parsing, token usage tracking, and per-user rate limiting.
Inputs to gather
- Feature name — e.g., "property-description-generator", "lead-scorer", "followup-writer"
- Input shape — what the endpoint receives (e.g., leadId, propertyId, free text)
- Output shape — what it returns (e.g., generated text, score object, match list)
- System prompt context — domain knowledge the AI needs (Saudi real estate, Arabic language, REGA rules)
- Model — claude-sonnet-4-6 (fast/cheap) or claude-opus-4-6 (best quality)
Steps
-
Check AI service layer exists. Read
apps/api/libs/ai-service.ts. If it doesn't exist, create it:// Anthropic client singleton, retry logic, token tracker import Anthropic from "@anthropic-ai/sdk"; const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }); -
Create the route file at
apps/api/routes/ai-{feature}.ts:- Import
authenticateTokenmiddleware - Validate input with zod
- Build messages array with system prompt + user content
- Call
client.messages.create()with structured output - Parse response, return JSON
- Track token usage:
prompt_tokens,completion_tokensinai_usagetable
- Import
-
Design the system prompt. Must include:
- Role: "You are a Saudi real estate expert assistant for عقاركم platform"
- Language: "Always respond in Saudi Arabic (العربية السعودية)"
- Domain constraints: REGA compliance, FAL license awareness, SAR currency
- Output format: specify JSON schema if structured output needed
-
Add rate limiting. In the route:
- Check
ai_usagetable: user's token count today - If > daily limit (default 50K tokens), return 429
- Track usage after successful call
- Check
-
Register the route in
apps/api/index.ts:import aiFeatureRoutes from "./routes/ai-{feature}"; app.use("/api/ai", aiFeatureRoutes); -
Create react-query hook using
/add-react-queryfor the frontend. -
Add API key to
.env:ANTHROPIC_API_KEY=sk-ant-...
Verification checklist
- Endpoint returns correct structured output
- System prompt produces Arabic responses
- Token usage is tracked per request
- Rate limiting rejects over-limit requests with 429
- Error handling: API timeout, invalid response, rate limit
-
/typecheckpasses
Anti-patterns
- Don't hardcode API keys — always use environment variables
- Don't stream responses for simple endpoints — use non-streaming for structured output
- Don't skip token tracking — you'll have no cost visibility
- Don't use opus for high-volume endpoints — use sonnet for speed/cost
- Don't let unauthenticated users hit AI endpoints (except chatbot)