Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

docs(ai): document the native Anthropic provider (#4243)

Josh Sanders6f331a62d64e
diff --git a/.env.example b/.env.example
index fd7f7fe6f4..4da492180e 100644
--- a/.env.example
+++ b/.env.example
@@ -25,7 +25,7 @@ OPENAI_ACCESS_TOKEN=
OPENAI_MODEL=
OPENAI_URI_BASE=
-# Optional: LLM token budget (applies to chat, auto-categorize, merchant detection, PDF processing).
+# Optional: LLM token budget for the OpenAI-compatible provider (chat, auto-categorize, merchant detection, PDF processing).
# Lower these for small-context local models (Ollama, LM Studio, LocalAI).
# For larger local models, raise the context window to match the model you actually run.
# Example: Gemma 3/4, Qwen, and other large-context models often need `LLM_CONTEXT_WINDOW=8192` or higher.
@@ -33,7 +33,7 @@ OPENAI_URI_BASE=
# LLM_MAX_RESPONSE_TOKENS=512
# LLM_MAX_HISTORY_TOKENS=
# LLM_SYSTEM_PROMPT_RESERVE=256
-# LLM_MAX_ITEMS_PER_CALL=25
+# LLM_MAX_ITEMS_PER_CALL=25 # Not a token budget: items per AI rule/merchant job, for every provider
# Optional: how long the chat waits for an assistant response before showing a
# "no response" error. Raise for slow local models — OpenAI-compatible providers
@@ -65,6 +65,17 @@ OPENAI_URI_BASE=
# OPENAI_SUPPORTS_RESPONSES_ENDPOINT= # Override Responses-API vs chat.completions routing
# LLM_JSON_MODE= # auto | strict | json_object | none
+# Optional: native Anthropic provider. LLM_PROVIDER picks the provider for chat
+# and background AI jobs, except categorization when Jev is selected (also
+# settable on the Self-Hosting settings page).
+# The token budget above doesn't trim or split Anthropic requests.
+# LLM_PROVIDER=anthropic # openai (default) | anthropic
+# ANTHROPIC_API_KEY= # ANTHROPIC_ACCESS_TOKEN also works and wins if both are set
+# ANTHROPIC_MODEL=claude-sonnet-4-6 # Required with ANTHROPIC_BASE_URL
+# ANTHROPIC_MAX_TOKENS=4096 # Reply limit, sent with every request
+# ANTHROPIC_REQUEST_TIMEOUT=600 # HTTP timeout in seconds
+# ANTHROPIC_BASE_URL= # Anthropic-compatible endpoint (e.g. a Bedrock or Vertex gateway)
+
# Optional: Jev (TypeSafe) — a classification provider used for transaction
# categorization. It answers typed questions with calibrated probabilities
# rather than generating text, so it is never used for chat or PDF processing
diff --git a/.env.local.example b/.env.local.example
index 5af0e08375..7d40bb320a 100644
--- a/.env.local.example
+++ b/.env.local.example
@@ -38,7 +38,7 @@ OPENAI_ACCESS_TOKEN =
OPENAI_URI_BASE =
OPENAI_MODEL =
-# LLM token budget. Applies to ALL outbound LLM calls: chat history,
+# LLM token budget. Applies to all OpenAI-compatible LLM calls: chat history,
# auto-categorize, merchant detection, provider enhancer, PDF processing.
# Defaults to Ollama's historical 2048-token baseline so small local models
# work out of the box — raise explicitly for cloud or larger-context models.
@@ -46,7 +46,7 @@ OPENAI_MODEL =
# LLM_MAX_RESPONSE_TOKENS = 512 # Reserved for the model's reply
# LLM_MAX_HISTORY_TOKENS = # Derived if unset (context - response - system_reserve)
# LLM_SYSTEM_PROMPT_RESERVE = 256 # Tokens reserved for the system prompt
-# LLM_MAX_ITEMS_PER_CALL = 25 # Upper bound on auto-categorize / merchant batches
+# LLM_MAX_ITEMS_PER_CALL = 25 # Not a token budget: items per AI rule/merchant job, for every provider
# OpenAI-compatible capability flags (custom/self-hosted providers)
# OPENAI_REQUEST_TIMEOUT = 60 # HTTP timeout in seconds; raise for slow local models
@@ -57,6 +57,14 @@ OPENAI_MODEL =
# OPENAI_SUPPORTS_RESPONSES_ENDPOINT = # true to force Responses API on custom providers
# LLM_JSON_MODE = # auto | strict | json_object | none
+# Native Anthropic provider. The token budget above doesn't trim or split its requests.
+# LLM_PROVIDER = anthropic # openai (default) | anthropic
+# ANTHROPIC_API_KEY = # ANTHROPIC_ACCESS_TOKEN also works and wins if both are set
+# ANTHROPIC_MODEL = claude-sonnet-4-6 # Required with ANTHROPIC_BASE_URL
+# ANTHROPIC_MAX_TOKENS = 4096 # Reply limit, sent with every request
+# ANTHROPIC_REQUEST_TIMEOUT = 600 # HTTP timeout in seconds
+# ANTHROPIC_BASE_URL = # Anthropic-compatible endpoint (e.g. a Bedrock or Vertex gateway)
+
# Document-search vector store. Local OpenAI-compatible chat endpoints usually
# do not implement OpenAI's hosted /v1/vector_stores API, so use pgvector and a
# separate embeddings endpoint when testing local AI.
diff --git a/docs/hosting/ai.md b/docs/hosting/ai.md
index 80b5d95ace..436d445d1c 100644
--- a/docs/hosting/ai.md
+++ b/docs/hosting/ai.md
@@ -19,23 +19,23 @@ Sure has **two separate AI systems** that operate independently. Understanding t
The interactive chat where users ask questions about their finances. Routes through one of two backends, or is turned off:
-- **Builtin** (default): Uses the OpenAI-compatible provider configured via `OPENAI_ACCESS_TOKEN` / `OPENAI_URI_BASE` / `OPENAI_MODEL`. Calls Sure's function tools directly (get_accounts, get_transactions, etc.).
+- **Builtin** (default): Uses the LLM provider selected by `LLM_PROVIDER`: the OpenAI-compatible provider (`OPENAI_ACCESS_TOKEN` / `OPENAI_URI_BASE` / `OPENAI_MODEL`, the default) or the [native Anthropic provider](#anthropic-native). Calls Sure's function tools directly (get_accounts, get_transactions, etc.).
- **External**: Delegates the entire conversation to a remote AI agent. The agent calls back to Sure via MCP to access financial data. Set `ASSISTANT_TYPE=external` as a global override, or configure each family's assistant type in Settings.
- **None in app** (`mcp_only`): Removes the in-app chat for the household: the right-hand chat sidebar and its toggle, the mobile Assistant tab, and the buttons that start a chat. Bring your own agent and connect it through [MCP](mcp.md), which keeps working. Set `ASSISTANT_TYPE=mcp_only` to remove the chat for every family.
### 2. Auto-Categorization and Merchant Detection (background)
-Background jobs that classify transactions and detect merchants. These **always** use the OpenAI-compatible provider (`OPENAI_ACCESS_TOKEN`), regardless of what the chat assistant uses. They rely on structured function calling with JSON schemas, not conversational chat.
+Background jobs that classify transactions and detect merchants. These use the LLM provider selected by `LLM_PROVIDER` (OpenAI-compatible or native Anthropic; categorization uses Jev instead when it's selected), regardless of what the chat assistant uses. They rely on structured function calling with JSON schemas, not conversational chat.
### What this means in practice
| Setting | Chat assistant | Auto-categorization |
|---------|---------------|---------------------|
-| `ASSISTANT_TYPE=builtin` (default) | Uses OpenAI provider | Uses OpenAI provider |
-| `ASSISTANT_TYPE=external` | Uses external agent | Still uses OpenAI provider |
+| `ASSISTANT_TYPE=builtin` (default) | Uses the LLM provider | Uses the LLM provider |
+| `ASSISTANT_TYPE=external` | Uses external agent | Still uses the LLM provider |
| `ASSISTANT_TYPE=mcp_only` | None in app; connect your own agent via MCP | Unchanged; uses the configured categorization provider |
-If you use an external agent for chat, you still need `OPENAI_ACCESS_TOKEN` set for auto-categorization and merchant detection to work. The two systems are fully independent.
+If you use an external agent for chat, you still need an LLM provider configured (`OPENAI_ACCESS_TOKEN` or `ANTHROPIC_API_KEY`) for auto-categorization and merchant detection to work. The two systems are fully independent.
With `mcp_only`, Sure also skips the features that need a user's AI consent: the LLM-written insight text and the bills AI shortcuts (review, smart fill, smart configuration).
@@ -167,6 +167,44 @@ Text extraction keeps its existing request behavior. Custom endpoints retain
their existing token parameters. Increase the LLM context budget for cloud
reasoning models; the default 2048-token context is intended for small local models.
+### Anthropic (native)
+
+Sure talks to Anthropic's Messages API directly. It serves chat (streamed), auto-categorization, merchant detection and enhancement, bill setup suggestions, and PDF imports (Claude reads the PDF itself; no vision setting needed).
+
+```bash
+LLM_PROVIDER=anthropic
+ANTHROPIC_API_KEY=your-anthropic-api-key # pipelock:ignore
+
+# Optional
+# ANTHROPIC_MODEL=claude-sonnet-4-6
+# ANTHROPIC_MAX_TOKENS=4096
+# ANTHROPIC_REQUEST_TIMEOUT=600
+# ANTHROPIC_BASE_URL=
+```
+
+Or in the UI: **Settings** → **Self-Hosting** → **AI Provider** → **Anthropic (Claude)**, then API Key, Base URL and Default Model. An env var disables its field; `LLM_PROVIDER` locks the provider choice.
+
+| Variable | Default | What it does |
+|----------|---------|--------------|
+| `LLM_PROVIDER` | `openai` | `anthropic` selects this provider. If the selected provider has no key, Sure uses the other one. While a custom OpenAI-compatible provider is configured (`OPENAI_URI_BASE` with an access token and model), chat still goes to it, so remove those settings when switching. |
+| `ANTHROPIC_API_KEY` | — | API key. `ANTHROPIC_ACCESS_TOKEN` also works and wins if both are set. |
+| `ANTHROPIC_MODEL` | `claude-sonnet-4-6` | Model for chat and all background jobs. Must start with `claude` unless a base URL is set. Claude Opus 5.5, Sonnet 5.5 and Fable 5.1 work for chat only until [#4125](https://github.com/we-promise/sure/issues/4125) is fixed. |
+| `ANTHROPIC_MAX_TOKENS` | `4096` | Reply limit, sent with every request (the Messages API requires one). |
+| `ANTHROPIC_REQUEST_TIMEOUT` | `600` | HTTP timeout per call, in seconds. |
+| `ANTHROPIC_BASE_URL` | `https://api.anthropic.com` | Anthropic-compatible endpoint. Requires `ANTHROPIC_MODEL`. |
+
+How it differs from the OpenAI-compatible provider:
+
+- **No token budget.** `LLM_CONTEXT_WINDOW` (default `2048`) does less here:
+ - **Every provider:** below `4096`, the chat's system prompt lists accounts and categories only as counts, and the model fetches them with tools.
+ - **OpenAI-compatible:** it is also the budget, with `LLM_MAX_RESPONSE_TOKENS` and `LLM_SYSTEM_PROMPT_RESERVE`, for splitting auto-categorization and merchant batches, and for trimming chat history on Chat Completions endpoints (`LLM_MAX_HISTORY_TOKENS`). The native OpenAI Responses API keeps history on OpenAI's side, so nothing is trimmed there.
+ - **Native Anthropic:** only the system-prompt check applies. History is sent whole, batches aren't split, and `LLM_MAX_RESPONSE_TOKENS`, `LLM_MAX_HISTORY_TOKENS` and `LLM_SYSTEM_PROMPT_RESERVE` are ignored. Set `LLM_CONTEXT_WINDOW` by env (e.g. `200000`) so the prompt lists accounts and categories; its UI field is in the OpenAI panel.
+- **The reply limit is always sent.** A reply that needs more than `ANTHROPIC_MAX_TOKENS` is cut off. Only bank statement extraction detects this (it logs a warning). Raise the limit if long answers or batch results come back incomplete.
+- **Batch size.** Auto-categorization, merchant detection and merchant enhancement send `LLM_MAX_ITEMS_PER_CALL` items per request (default `25`), with no splitting to fit the context window. Raise `ANTHROPIC_MAX_TOKENS` along with it, or replies are cut off (see [Where AI jobs run](#where-ai-jobs-run-sidekiq-lanes)).
+- **Its own timeout.** `ANTHROPIC_REQUEST_TIMEOUT` replaces `OPENAI_REQUEST_TIMEOUT`. `AI_RESPONSE_TIMEOUT` still covers the whole chat turn. `OPENAI_EXTRA_HEADERS`, `LLM_JSON_MODE` and `OPENAI_SUPPORTS_*` don't apply.
+
+**Bedrock, Vertex and proxies:** `ANTHROPIC_BASE_URL` can point at any endpoint that speaks the Anthropic Messages API, such as a gateway in front of AWS Bedrock or Google Vertex AI. Set `ANTHROPIC_MODEL` to that endpoint's model ID (for Bedrock, e.g. `anthropic.claude-sonnet-4-5-20250929-v1:0`); the `claude` prefix check is skipped. Sure sends the key as an Anthropic API key (`x-api-key`) and doesn't sign AWS or Google requests itself.
+
### Google Gemini
You can connect to Google Gemini either directly via Google AI Studio's OpenAI-compatible endpoint or via OpenRouter.
@@ -205,7 +243,9 @@ OPENAI_MODEL=google/gemini-3.8-flash
- Automatic fallbacks
- Usage tracking
-### Anthropic Claude (via OpenRouter)
+### Anthropic Claude via OpenRouter (OpenAI-compatible)
+
+This route uses the OpenAI-compatible provider and its token budget. To call Anthropic directly, use the [native Anthropic provider](#anthropic-native).
```bash
OPENAI_ACCESS_TOKEN=your-openrouter-api-key
@@ -417,8 +457,8 @@ Similar requirements to categorization:
For self-hosted deployments, you can configure AI settings through the web interface:
1. Go to **Settings** → **Self-Hosting**
-2. Scroll to the **AI Provider** section
-3. Configure the provider:
+2. Scroll to the **AI Provider** section and choose **OpenAI** or **Anthropic (Claude)**
+3. Configure the provider (OpenAI fields shown; for Anthropic see [Anthropic (native)](#anthropic-native)):
- **Access Token** - Your API key
- **API Base URL** - Custom endpoint (leave blank for OpenAI)
- **Model** - Model name (required for custom endpoints)