AI & Model Integration
Relevant source files
The following files were used as context for generating this wiki page:
- server/polar/organization_review/analyzer.py
- clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts
- clients/apps/web/src/app/(main)/feedback/question/route.ts
- clients/apps/web/src/app/(main)/onboarding/validate-description/route.ts
- server/polar/backoffice/organizations_v2/views/sections/overview_section.py
- clients/apps/web/e2e/healing/prompts/e2e-checkout-healing.md
Polar integrates Large Language Models (LLMs) across multiple operational layers to automate complex tasks, including compliance reviews, product configuration assistance, customer support, and automated E2E test healing. The system utilizes a multi-model strategy, leveraging providers like Anthropic (Claude), Google (Gemini), and OpenAI via the Vercel AI SDK and a Pydantic AI Gateway.
Sources: clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts:24-43, server/polar/organization_review/analyzer.py:461-477
Architecture and Gateways
Polar uses a centralized proxy approach for LLM interactions through the PYDANTIC_AI_GATEWAY_API_KEY. This gateway manages traffic to various providers, ensuring unified authentication and monitoring.
Model Providers and Infrastructure
The project utilizes the ai (Vercel AI SDK) and pydantic-ai libraries to interface with models. Key models mentioned include:
- Anthropic:
claude-sonnet-4-6for complex reasoning and conversational tasks. - Google:
gemini-3.1-flash-lite-previewandgemini-3-flash-previewfor rapid classification and routing. - OpenAI:
gpt-5.4-mini(referenced in onboarding validation).
Sources: clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts:24-43, clients/apps/web/src/app/(main)/onboarding/validate-description/route.ts:18-21
Model Context Protocol (MCP)
For product creation, Polar implements the Model Context Protocol (MCP). This allows the LLM to access server-side tools dynamically via a StreamableHTTPClientTransport.
AI-Driven Compliance and Risk Analysis
The ReviewAnalyzer in the backend serves as the core engine for organization risk assessment. It evaluates five dimensions: Policy Compliance, Product Legitimacy, Identity/Trust, Financial Risk, and Setup Readiness.
Risk Assessment Dimensions
| Dimension | Focus Area | Source |
|---|---|---|
| Policy Compliance | Alignment with Acceptable Use Policy (AUP) | analyzer.py:27 |
| Product Legitimacy | Cross-referencing website data with Polar listings | analyzer.py:43 |
| Identity & Trust | Verification status and social presence | analyzer.py:53 |
| Financial Risk | Refund rates, chargebacks, and risk scores | analyzer.py:68 |
| Setup Readiness | Validating delivery mechanisms (webhooks, benefits) | analyzer.py:86 |
Automated Decision Logic
The analyzer supports multiple review contexts: SUBMISSION, THRESHOLD, MANUAL, and APPEAL. Each context uses a specific preamble to calibrate the model's sensitivity and data focus.
Conversational AI Features
AI Product Assistant
The product creation flow uses a router-agent pattern. A "lite" model first determines if the user's request is relevant and if tool access is required before a more capable model (Sonnet) takes over for tool execution.
Key Components:
- Router Prompt: Determines relevance, manual setup needs, and tool requirements.
- Shared System Prompt: Contains detailed knowledge about Polar's Merchant of Record model, benefit types (License keys, Feature flags), and pricing logic.
- Tools:
markAsDone,redirectToManualSetup, and dynamically discovered MCP tools for managing meters and products.
Support Assistant (Polar Support AI)
Located in the feedback module, this assistant uses a tool-calling workflow to search documentation and escalate to humans when necessary.
Available Tools:
search: Queries Mintlify docs.fetchPageContent: Retrieves full Markdown for grounded answers.escalateToHuman: Opens the support panel for bugs or complex queries.
Sources: clients/apps/web/src/app/(main)/feedback/question/route.ts:140-205
Specialized AI Workflows
Onboarding Description Validation
A specific implementation for validating seller descriptions against the Acceptable Use Policy (AUP). It outputs a structured verdict: APPROVE, DENY, or CLARIFY.
Automatic DENY categories include:
- Adult/pornographic content
- Firearms/weapons
- Watermark removal tools
- MLM/Pyramid schemes
Sources: clients/apps/web/src/app/(main)/onboarding/validate-description/route.ts:145-155
E2E Test Healing
The system includes prompts for analyzing E2E flakiness. An AI agent uses Chrome DevTools MCP to:
- Read failing test specs.
- Reproduce failures in a real browser.
- Classify failures as
flaky,regression, orenvironment. - Propose fixes for flaky tests by modifying Playwright code.
Sources: clients/apps/web/e2e/healing/prompts/e2e-checkout-healing.md:1-50
Implementation Details
Data Snapshots
For compliance reviews, the system generates a DataSnapshot containing:
- Organization Details: Name, website, about, and social links.
- Products: Names, prices (including pay-what-you-want and ad-hoc pricing), and descriptions.
- Setup Signals: Webhook configurations, success/return URLs, and domain consistency checks.
- Metrics: Succeeded payments, refund rates, and dispute counts.
Sources: server/polar/organization_review/analyzer.py:539-650
Feedback Rendering
The results of AI reviews are rendered in the Backoffice UI, showing the verdict, risk levels per dimension, and the raw data snapshot used for the decision.
Sources: server/polar/backoffice/organizations_v2/views/sections/overview_section.py:126-215
Monitoring and Tracing
Polar uses PostHog for AI tracing, wrapping model calls with withTracing to log user IDs, conversation IDs, and organization groups.
Sources: clients/apps/web/src/app/(main)/dashboard/[organization]/(header)/products/new/ai/chat/route.ts:302-313, clients/apps/web/src/app/(main)/onboarding/validate-description/route.ts:77-81
Conclusion
The AI integration in Polar is a sophisticated multi-layered system that balances automation with human oversight. By utilizing specialized agents for compliance, configuration, and support, the platform handles complex Merchant of Record responsibilities while maintaining a developer-centric experience. Significant emphasis is placed on grounding AI responses in real-time documentation and internal data snapshots to minimize hallucinations and ensure policy alignment.