Skip to main content

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 in src/ai/genkit.ts:
Key Configuration:
  • Plugin: @genkit-ai/google-genai for Gemini integration
  • Default Model: gemini-2.5-flash (fast, cost-effective)
  • API Key: Read from process.env.GOOGLE_GENAI_API_KEY
You can override the model per-flow by specifying model: in flow definitions.

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

File: src/ai/flows/generate-argument-blueprint.tsPurpose: Main analysis flow that generates complete argument mapsInput Schema:
Output Schema:
Processing Steps:
  1. Generate search query (Gemini)
  2. Web search (Firecrawl tool)
  3. Scrape articles (Firecrawl tool)
  4. Twitter search (Twitter API tool)
  5. Main analysis (Gemini with full context)
  6. Social pulse summary (Gemini)
  7. Schema validation (Zod)

Prompt Engineering

Main Analysis Prompt

The core prompt for argument blueprint generation:

Prompt Techniques

Pattern: Provide 2-3 examples before asking for analysis
Benefit: Improves consistency and output quality

Tool Integration

Genkit’s tool system enables AI to call external functions during generation.

Tool Definition Pattern

Registered Tools

  1. webSearch - Firecrawl API search
  2. webScraper - Article content extraction
  3. twitterSearch - Social sentiment gathering
Tool Calling Flow:
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
Our Strategy:
  • 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 articles

Response Parsing

JSON Extraction

AI responses may wrap JSON in markdown code blocks:

Schema Validation

Error Handling

Retry Logic

Fallback Strategies

Trigger: Firecrawl returns 0 resultsFallback:
  1. Try broader search query
  2. Use AI knowledge-only mode
  3. Display disclaimer about missing sources
Trigger: JSON extraction or Zod validation failsFallback:
  1. Retry generation with stronger prompt
  2. Use regex to extract partial data
  3. Return error to user with raw text
Trigger: API returns 429 errorFallback:
  1. Implement exponential backoff
  2. Queue request for later
  3. Show user estimated wait time

Performance Optimization

Parallel Processing

Impact: Reduces total latency from 20s to 12s

Streaming Responses (Future)

Benefit: User sees progress in real-time

Observability

Genkit Dev UI

Run npm 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
Genkit UI runs on http://localhost:4000 separate from the main app.

Logging Strategy

Production: Replace with Pino or Winston for structured JSON logs

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