Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

docs: clarify local LLM context window tuning (#2661)

Sure Admin (bot)f62c805c69af
diff --git a/.env.example b/.env.example
index 3009d1e229..c84d0e414e 100644
--- a/.env.example
+++ b/.env.example
@@ -27,7 +27,8 @@ OPENAI_URI_BASE=
# Optional: LLM token budget (applies to chat, auto-categorize, merchant detection, PDF processing).
# Lower these for small-context local models (Ollama, LM Studio, LocalAI).
-# Defaults work for modern cloud OpenAI models without configuration.
+# 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.
# LLM_CONTEXT_WINDOW=2048
# LLM_MAX_RESPONSE_TOKENS=512
# LLM_MAX_HISTORY_TOKENS=
diff --git a/compose.example.yml b/compose.example.yml
index 108fd6b9a8..b932196246 100644
--- a/compose.example.yml
+++ b/compose.example.yml
@@ -58,6 +58,10 @@ x-rails-env: &rails_env
REDIS_URL: redis://redis:6379/1
# NOTE: enabling OpenAI will incur costs when you use AI-related features in the app (chat, rules). Make sure you have set appropriate spend limits on your account before adding this.
OPENAI_ACCESS_TOKEN: ${OPENAI_ACCESS_TOKEN}
+ OPENAI_MODEL: ${OPENAI_MODEL:-}
+ OPENAI_URI_BASE: ${OPENAI_URI_BASE:-}
+ LLM_CONTEXT_WINDOW: ${LLM_CONTEXT_WINDOW:-}
+ OPENAI_REQUEST_TIMEOUT: ${OPENAI_REQUEST_TIMEOUT:-60}
services:
web:
diff --git a/docs/hosting/ai.md b/docs/hosting/ai.md
index 92e17bb481..32912fc1d9 100644
--- a/docs/hosting/ai.md
+++ b/docs/hosting/ai.md
@@ -216,6 +216,13 @@ OPENAI_URI_BASE=http://localhost:11434/v1
# Model you pulled
OPENAI_MODEL=llama3.1:13b
+# Raise this for large-context local models so auto-categorize and merchant detection
+# have enough prompt budget for categories + schemas before transaction rows are added.
+LLM_CONTEXT_WINDOW=8192
+
+# Slow local models often need a longer HTTP timeout once the prompt budget issue is fixed.
+OPENAI_REQUEST_TIMEOUT=180
+
# Optional: enable debug logging in the AI chat
AI_DEBUG_MODE=true
```
@@ -224,6 +231,8 @@ AI_DEBUG_MODE=true
- You **must** set `OPENAI_MODEL` - the system cannot default to `gpt-4.1` as that model won't exist in Ollama
- The `OPENAI_ACCESS_TOKEN` can be any non-empty value (Ollama ignores it)
- If you don't set a model, chats will fail with a validation error
+- Auto-categorization uses a conservative default `LLM_CONTEXT_WINDOW=2048`, so large category lists or schemas can exhaust the prompt budget before any transactions are sent
+- If requests start timing out after raising `LLM_CONTEXT_WINDOW`, increase `OPENAI_REQUEST_TIMEOUT` too; these are separate limits
### Docker Compose Example
@@ -234,6 +243,8 @@ services:
- OPENAI_ACCESS_TOKEN=ollama-local
- OPENAI_URI_BASE=http://ollama:11434/v1
- OPENAI_MODEL=llama3.1:13b
+ - LLM_CONTEXT_WINDOW=8192
+ - OPENAI_REQUEST_TIMEOUT=180
- AI_DEBUG_MODE=true # Optional: enable debug logging in the AI chat
depends_on:
- ollama
@@ -1024,6 +1035,34 @@ ollama list # See what's installed
ollama pull model-name # Install a model
```
+### "Fixed prompt tokens exceed context budget"
+
+**Symptom:** Auto-categorization or merchant detection fails immediately with an error like:
+
+```text
+Fixed prompt tokens (2108) exceed context budget (1280)
+```
+
+**Cause:** Sure computes the usable prompt budget as:
+
+```text
+context_window - max_response_tokens - system_prompt_reserve
+```
+
+The defaults are conservative:
+- `LLM_CONTEXT_WINDOW=2048`
+- `LLM_MAX_RESPONSE_TOKENS=512`
+- `LLM_SYSTEM_PROMPT_RESERVE=256`
+
+That leaves `1280` input tokens. On local or custom models, the fixed prompt can already exceed that budget once you include Sure's instructions, category taxonomy, and schema payloads.
+
+**Fix:**
+```bash
+LLM_CONTEXT_WINDOW=8192
+```
+
+Then restart both `web` and `worker` so the new env var is loaded. If you are using Docker Compose, make sure your compose file forwards `LLM_CONTEXT_WINDOW` into the containers.
+
### Slow Responses
**Symptom:** Long wait times for AI responses
@@ -1038,6 +1077,7 @@ ollama pull model-name # Install a model
- Try a smaller model
- Ensure you're using GPU, not CPU
- Check for thermal throttling
+- If you see `Net::ReadTimeout` after fixing the context budget, raise `OPENAI_REQUEST_TIMEOUT` (for example `180`)
### No Provider Available