Metanow Docs
Contributing

AI Providers

How to configure the active AI provider and how to add a new one.

All AI calls in MetaNow Console flow through a single abstraction: ModelRouterService. No module code imports a provider SDK directly.

Configuring the active provider

Set AI_PROVIDER in your .env:

AI_PROVIDER=anthropic   # default
AI_PROVIDER=gemini

The AiProviderFactory resolves the provider at startup based on AI_PROVIDER and the available API keys:

AI_PROVIDER=anthropic
  ANTHROPIC_API_KEY set?  → AnthropicAiProvider
  else GEMINI_API_KEY set? → GeminiAiProvider (fallback)
  else                     → NoopAiProvider (no AI, safe for tests)

AI_PROVIDER=gemini
  GEMINI_API_KEY set?      → GeminiAiProvider
  else ANTHROPIC_API_KEY?  → AnthropicAiProvider (fallback)
  else                     → NoopAiProvider

If neither key is set, the server boots with NoopAiProvider — generation calls return empty structured output without crashing. This makes it safe to run integration tests and CI pipelines without live AI credentials.

Default model

All modules default to Claude Sonnet 4.6 (claude-sonnet-4-6). This is specified in two places:

  1. apps/server/src/ai/learning-contract.loader.ts — the Stage 0 default constant.
  2. Each module's learning_contract.yaml — the model_routing.default field.

Currently only the brief_engine module has a learning_contract.yaml (apps/server/src/briefs/learning_contract.yaml). All other modules fall back to the Stage 0 default via loadModelRouting().

How generation works end to end

Caller (e.g. BriefPromptsProcessor)
  │
  ▼
ModelRouterService.generate(input)
  │
  ├── 1. loadModelRouting(module) → resolves model ID from contract YAML
  ├── 2. buildSystemPrompt()      → assembles instructions
  ├── 3. buildUserMessage()       → context + retrieved past decisions
  ├── 4. AiProviderFactory.getProvider().generateRecommendation(...)
  ├── 5. writeTrace()             → inserts row to ai_decision_trace
  └── 6. returns RouterOutput { chosen, alternativesConsidered, reasoning,
                                 confidence, tokensUsed, costMillicents, latencyMs,
                                 modelProvider, modelVersion, traceId }

The RouterOutput shape is identical regardless of provider, so callers are fully decoupled from the underlying model.

Adding a new provider

Implement the AiProvider interface — create a new file in apps/server/src/ai/, e.g. my-provider-ai.provider.ts. Implement the single required method:

interface AiProvider {
  generateRecommendation(input: GenerateInput): Promise<GenerateOutput>;
}

GenerateOutput must include chosen, alternativesConsidered, reasoning, confidence, tokensUsed, costMillicents (integer millicents, never a float), latencyMs, modelProvider, and modelVersion.

Register it in AiModule — add the provider class to the providers array in apps/server/src/ai/ai.module.ts.

Inject it into AiProviderFactory — add a constructor parameter and a new case in getProvider().

Add the new enum value to AI_PROVIDER — extend the Zod schema in packages/env/src/server.ts:

AI_PROVIDER: z.enum(["anthropic", "gemini", "my-provider"]).default("anthropic"),

Add a NoopAiProvider test path — ensure the getProvider() fallback chain still reaches NoopAiProvider when no keys are set, so CI continues to work without credentials.

Rate limiting

AI generation is rate-limited per organisation by OrgThrottlerGuard:

  • Window: AI_RATE_LIMIT_TTL seconds (default: 3600 — one hour).
  • Limit: AI_RATE_LIMIT_MAX calls per org per window (default: 20).

Requests that exceed the limit return 429 Too Many Requests. Adjust both values in .env as needed for your deployment.

On this page