# AI layers

Where the embedding and the LLM can run, what leaves the machine, and how to turn them on.

## AI layers: where the model runs

Two layers can use an AI model: the embedding of `tkt search` (items close in meaning join the hits of the words) and the LLM of `--summary`, `tkt suggest` and `tkt plan from-docs`. Each can run in three places, and each says what leaves which boundary:

- **this machine**: the local model (`aitodo[embed]`) or a local LLM command; nothing leaves the machine (but the default `claude -p` command sends the text to Anthropic on your own account);
- **own infrastructure**: a company server or the company's cloud (a company Ollama, a company endpoint behind the tkt server); the text stays inside the company;
- **tkt server**: the project's server embedding and LLM; the text leaves the company's boundary.

With nothing set, `tkt search` embeds nothing and asks no server: store text goes to the tkt server only when a setting says so (`tkt config set embedProvider server`; it needs `tkt project llm enable` on the project and the `llm` grant on your token). `tkt ai doctor` checks whether the server can embed and, if it can, suggests that. The LLM commands use the project's server LLM by default when it is on and you have the `llm` grant. Nothing has to be installed.

- `tkt ai doctor`: first a short summary per layer: the provider, the setting that chose it, where the text goes and, when the provider in use fails, the failure with its fix. Then this machine (OS, RAM, GPU, as facts: a local model is an optional extra) and a check of each provider: only the chosen one can be `fail`, the others are `available` or `not set up`, with the fix as a hint. The steps for every option come with `--all`, without it only a failing layer's. `--live` sends one fixed text to the providers in use.
- `tkt search --embed-provider local|server|off`; `--llm-provider local|server|off` on the LLM commands.
- Every `tkt search` that embedded says where the text went (`--json`: `embedding`).

### Details

- **Settings, highest first:** the flag; `AITODO_EMBED_PROVIDER` / `AITODO_LLM_PROVIDER` (and `AITODO_LLM_CMD`); the user `config.yaml` next to `credentials.yaml` (`AITODO_CONFIG_DIR`, `~/.config/aitodo/`, `%APPDATA%\aitodo`: yours, never committed); the store config (`tkt config set embedProvider local`, `embedModel`, `llmProvider`: the team's, committed); the default. A bad value: `ERR_AI_CONFIG_INVALID` naming the env var (exit 2) or the file.
- **Fallback:** `local` missing (`aitodo[embed]` not installed) or failing means no embedding (never the server instead), with a notice on stderr; `ERR_EMBED_PROVIDER` says why. `server` without a pin or a token exits 2 (`tkt project register`, `tkt login`); a server that refuses or is out of reach (AI off, no grant, a cap, `ERR_LLM_UPSTREAM`, `--offline`) is skipped with a notice. A search never fails on the embedding.
- **Secrets:** both files hold only which provider and which model. Keys live in env vars or the OS keychain: `tkt config set` refuses one (`ERR_AI_SECRET_IN_CONFIG`), and `tkt ai doctor` warns about a key-like entry in either file (it names the key, never the value). A search that sends text that looks like a key or token past this machine warns with the count.
- **Cache:** the vectors are kept in `.aitodo/cache/search/embeddings.db` by project, model and text; another model embeds again, never mixes.
- **Server-hosted store:** `tkt search` runs on the server and uses only the project's server embedding (the run's grant); the user and store settings do not apply, `--embed-provider off` does.
- **Own endpoint** (OpenAI-compatible: vLLM, Ollama, Azure OpenAI): `tkt config set embedProvider endpoint` with `embedUrl` and `embedModel`, for the LLM `llmProvider: endpoint` with `llmUrl` and `llmModel` (or `AITODO_EMBED_URL`, `AITODO_LLM_URL`, `AITODO_LLM_MODEL`); the key only in `AITODO_EMBED_KEY` and `AITODO_LLM_KEY`. A loopback URL is this machine, any other own infrastructure. A missing URL or model exits 2; the answer is checked against its schema, with one retry.
- **Endpoints outside this machine need https** (`ERR_AI_ENDPOINT_INSECURE`); plain http only to localhost, and no password in the URL.

## Local LLM with Ollama

tkt ships a wrapper that makes a running Ollama the LLM command: it sends the prompt with the answer's schema to Ollama's `/api/chat` and prints the JSON. `tkt ai doctor` prints the exact line, with the Python that runs `tkt`:
`ollama pull qwen3.5:9b-q4_K_M`, then `AITODO_LLM_CMD='<python> -m aitodo.client.ollama_llm {schema}'`.

- Use the GGUF tag (`qwen3.5:9b-q4_K_M`): on Apple Silicon the plain tag runs the MLX build, whose Homebrew package lacks structured output.
- `OLLAMA_HOST_URL` (default `http://127.0.0.1:11434`): this machine, or a company Ollama server over https (own infrastructure); `OLLAMA_MODEL`, `OLLAMA_NUM_CTX` (Ollama's own context is small for a long prompt), `OLLAMA_STATS_FILE` (one line of counts and times per call; never text).
- Keep Ollama on `127.0.0.1`: its API has no authentication. A shared company server needs a proxy with authentication in front.
- A local model is fine for the answer step; picking from a long list needs a strong model (`tkt ai doctor` says what was measured).

## Commands on this page

- [`tkt ai doctor`](/en/docs/reference/commands/projects.md#tkt-ai-doctor): Show the AI layers (embedding, LLM): a summary of the provider each uses, the setting that chose it, where the text goes and whether it fails; then this machine, a check of each provider with its fix, and the steps to set up each option

## Related

- Env var: [`AITODO_EMBED_PROVIDER`](/en/docs/reference/env.md#AITODO_EMBED_PROVIDER): local, server, endpoint or off: the embedding provider of tkt search; beats embedProvider in the user and store config
- Env var: [`AITODO_EMBED_MODEL`](/en/docs/reference/env.md#AITODO_EMBED_MODEL): The embedding model: the local model (a sentence-transformers name) or the endpoint's; beats embedModel
- Env var: [`AITODO_EMBED_URL`](/en/docs/reference/env.md#AITODO_EMBED_URL): The OpenAI-compatible embedding endpoint of embedProvider: endpoint (https outside this machine); beats embedUrl
- Env var: [`AITODO_EMBED_KEY`](/en/docs/reference/env.md#AITODO_EMBED_KEY): The key of that embedding endpoint, if it needs one; a secret, never in a config file
- Env var: [`AITODO_LLM_URL`](/en/docs/reference/env.md#AITODO_LLM_URL): The OpenAI-compatible LLM endpoint of llmProvider: endpoint (https outside this machine); beats llmUrl
- Env var: [`AITODO_LLM_MODEL`](/en/docs/reference/env.md#AITODO_LLM_MODEL): The model llmProvider: endpoint asks for; beats llmModel
- Env var: [`AITODO_LLM_KEY`](/en/docs/reference/env.md#AITODO_LLM_KEY): The key of that LLM endpoint, if it needs one; a secret, never in a config file
- Config key: [`embedProvider`](/en/docs/reference/config.md#embedProvider): The embedding provider of tkt search: local, server, endpoint or off (unset: none; the server only when set)
- Config key: [`embedModel`](/en/docs/reference/config.md#embedModel): The embedding model: a sentence-transformers name for local (unset: BAAI/bge-m3), the endpoint's model for endpoint
- Config key: [`embedUrl`](/en/docs/reference/config.md#embedUrl): The OpenAI-compatible embedding endpoint of embedProvider: endpoint (https; the key in AITODO\_EMBED\_KEY)
- Config key: [`llmProvider`](/en/docs/reference/config.md#llmProvider): The LLM provider: local (the LLM command), server, endpoint or off (unset: the tkt server's when available)
- Config key: [`llmUrl`](/en/docs/reference/config.md#llmUrl): The OpenAI-compatible LLM endpoint of llmProvider: endpoint (https; the key in AITODO\_LLM\_KEY)
