Overview
Argument Cartographer’s AI layer is built on Google Genkit, a TypeScript framework for building AI-powered applications. This architecture provides type-safe LLM interactions, tool calling, and observability out of the box.Current Model: Gemini 2.5 Flash - optimized for speed and cost while maintaining high quality reasoning.
Genkit Configuration
The core AI instance is configured insrc/ai/genkit.ts:
- Plugin:
@genkit-ai/google-genaifor Gemini integration - Default Model:
gemini-2.5-flash(fast, cost-effective) - API Key: Read from
process.env.GOOGLE_GENAI_API_KEY
AI Flows
Genkit Flows are the primary abstraction for AI tasks. Each flow represents a complete AI workflow with inputs, outputs, and intermediate steps.Flow Architecture
Core Flows
- Generate Argument Blueprint
- Identify Logical Fallacies
- Ask More
- Summarize Source
- Explain Fallacy
File: Output Schema:Processing Steps:
src/ai/flows/generate-argument-blueprint.tsPurpose: Main analysis flow that generates complete argument mapsInput Schema:- Generate search query (Gemini)
- Web search (Firecrawl tool)
- Scrape articles (Firecrawl tool)
- Twitter search (Twitter API tool)
- Main analysis (Gemini with full context)
- Social pulse summary (Gemini)
- Schema validation (Zod)
Prompt Engineering
Main Analysis Prompt
The core prompt for argument blueprint generation:Prompt Techniques
- Few-Shot Learning
- Chain of Thought
- Constrained Generation
- Role Prompting
Pattern: Provide 2-3 examples before asking for analysisBenefit: Improves consistency and output quality
Tool Integration
Genkit’s tool system enables AI to call external functions during generation.Tool Definition Pattern
Registered Tools
- webSearch - Firecrawl API search
- webScraper - Article content extraction
- twitterSearch - Social sentiment gathering
In the current implementation, tools are called programmatically rather than via AI function calling to maintain deterministic control flow.
Context Management
Token Budget Strategy
Gemini 2.5 Flash Limits:- Input: 1M tokens
- Output: 8K tokens
- Allocate 20K tokens max for source context
- 12K chars per source × 8 sources = ~96K chars = ~24K tokens
- Leaves room for prompt, examples, and safety margin
Context Prioritization
When sources exceed budget:1
Prioritize by Domain
Trusted outlets (Reuters, BBC) get full content
2
Truncate Less Reliable
Reduce token allocation for lower-quality sources
3
Summarize If Needed
Use
summarizeSourceText flow for lengthy articlesResponse Parsing
JSON Extraction
AI responses may wrap JSON in markdown code blocks:Schema Validation
Error Handling
Retry Logic
Fallback Strategies
No Web Results
No Web Results
Trigger: Firecrawl returns 0 resultsFallback:
- Try broader search query
- Use AI knowledge-only mode
- Display disclaimer about missing sources
Parsing Failure
Parsing Failure
Trigger: JSON extraction or Zod validation failsFallback:
- Retry generation with stronger prompt
- Use regex to extract partial data
- Return error to user with raw text
Rate Limit Hit
Rate Limit Hit
Trigger: API returns 429 errorFallback:
- Implement exponential backoff
- Queue request for later
- Show user estimated wait time
Performance Optimization
Parallel Processing
Streaming Responses (Future)
Observability
Genkit Dev UI
Runnpm run genkit:dev to access:
- Flow Inspector: View all registered flows
- Trace Viewer: See execution steps and timing
- Input/Output Tester: Test flows with sample data
- Model Switcher: Try different Gemini models
Logging Strategy
Next Steps
External Integrations
Learn how Firecrawl, Twitter, and Gemini APIs are integrated
Data Layer
Understand data persistence and security
Configuration
Customize AI model, prompts, and behavior
API Reference
Detailed API documentation for all flows
