ADR 0027: An optional AI copilot works through tool calls, not retrieval
Status: Superseded by ADR 0041. Its rule that every warehouse decision is made by deterministic code is superseded by ADR 0064. The copilot, its three model providers and everything below were retired and deleted; the text stays as the record of what was decided and why. The paths, symbols and configuration keys in Evidence refer to the historical tree at commit 4594abf2, not the current tree.
Context
Warehouse staff ask questions whose answers are in Karyo's own data: what is expiring, which orders are open, what needs replenishing, what just happened. A language model can turn a question like that into the right queries and a readable answer - but only if it reads current data. An answer built on stale or guessed numbers is worse than no answer, because someone will act on it. Some of those questions end in an action, such as receiving or moving stock, which must not happen on a model's say-so.
Warehouse operations themselves - selecting stock, finding a location, releasing work - have to be deterministic and must not depend on a model at all.
Decision
- Karyo includes an optional copilot, in
karyo-ai-core, served at/api/v1/ai. It is off unless configured:karyo.ai.provider(KARYO_AI_PROVIDER) selectsanthropic,openrouter,ollamaornone, andnoneis the default. Withnone,POST /api/v1/ai/chatanswers 503. - It is built on LangChain4j through its Quarkus extension. Three named models are fixed when
the application is built -
claudeon Anthropic,openrouterthrough LangChain4j's OpenAI-compatible model pointed at OpenRouter's base URL, andlocalon Ollama - and the runtime setting chooses between them. There is no automatic fallback from one provider to the other. - OpenRouter is the third provider because it is one key to many model families. It needs no
account on each model vendor, and its OpenAI-compatible endpoint lets the same named model
later point at any other OpenAI-compatible service without a code change. Its default model is
DeepSeek V4 Flash (
deepseek/deepseek-v4-flash-0731), a hosted model that is cheap per token and supports Karyo's tool calls; the id was confirmed against OpenRouter's live model catalog on 2026-09-25 (batch and free variants excluded). Operators override it per installation withKARYO_AI_OPENROUTER_MODEL. OpenRouter's key comes only from theOPENROUTER_API_KEYenvironment variable - never a config file, never a database row, and never logged (request and response logging stay at their default off). - The default Anthropic model is the current Opus id, not a retired one. The shipped
claude-opus-4-20250514was retired by Anthropic on 2026-06-15; the default is nowclaude-opus-5-5, confirmed from Anthropic's model documentation on 2026-09-25 (Active, no retirement before 2027-09-22). Operators override withKARYO_AI_ANTHROPIC_MODEL. - Every named model carries explicit
timeoutand token caps. Anthropic and OpenRouter: 30 s andmax-tokens4096. Ollama: 30 s andnum-predict4096 (Ollama's name for the token cap). A slow provider cannot hang a request indefinitely and a verbose reply cannot run away; the limits are recorded in the Evidence section. - A hung provider answers the 502 problem, not the proxy's 504. Anthropic and OpenRouter make
one attempt per model call (
max-retries0), so a hung provider costs one timeout; Ollama's client makes three attempts and Quarkus exposes no setting for it. The shipped nginx gives/api/v1/ai/a 120 s read timeout instead of the default 60 s, so the caller receivescopilot-provider-unavailablerather than an nginx 504 page. An operator who raises aKARYO_AI_*_TIMEOUTkeeps it (three times it for Ollama) below 120 s. - The model answers by calling tools, and the tools call Karyo's services in process, under the caller's identity: read tools, insight tools - including recent warehouse activity, read from the outbox (ADR 0009) - and action tools.
- An action tool never acts. It checks the caller's write role and stores a proposal. The action
runs only when the same user confirms it (
POST /api/v1/ai/confirm/{id}), and the confirmation checks ownership and then the write role again before anything executes. The same user can instead decline it (POST /api/v1/ai/decline/{id}, the same two checks), which discards it unexecuted. Either decision is recorded in that owner's chat memory as one message - approved with the executor's result, including any created id, failed with the reason, or declined - so a dependent step prepared in a later turn works from the concrete result; a 404 or 403 records nothing. - A chat reply returns every live proposal of this reply, not only the newest. A reply may
carry several proposals - one confirm card each, confirmed independently - but only for
independent actions. The model is instructed not to prepare a step that depends on the
result of an unexecuted proposal (for example a putaway of a stock unit that only the receive
creates): it prepares the first step and the dependent step in a later turn, after the user
confirms (CopilotPrompt.SYSTEM, and the suggested prompts are single-step). Nothing in the
tools or the resource enforces that instruction, so each card's summary is what the user
approves. For ADR 0017 additive
compatibility the reply also keeps
proposal- the newest proposal of this reply, or null - besideproposals;proposalis deprecated and will not be removed within v1. - Chat memory is bound to its owner. The model bean's memory id is composed server-side from
clientId,usernameand the client-chosensessionId, so two callers who pick the same session id get two transcripts instead of one shared - and possibly cross-tenant - context. The copilot beans are application-scoped, so a session keeps its earlier turns across requests.CopilotServiceforgets a conversation idle for 30 minutes (no chat turn, approve or decline): the model bean drops its memory handle and the stored messages, so memory stays bounded by recent activity. A conversation runs one turn at a time, and a recorded approve or decline waits for the turn in flight, so no write lands between a tool call and its result. The chat input is bounded by request validation:messageandsessionIdmust not be blank and are capped (@Size4000 and 200). A reply's proposals are exactly those its own request's tools prepared, so an earlier turn's, an overlapping request's or another user's proposal on the same session id never appears in it. - A configured provider that does not answer is a named 502 problem.
POST /api/v1/ai/chatmaps provider failures (unreachable, timeout, refused key, retired model id) to RFC 7807https://karyowms.com/errors/copilot-provider-unavailablewith status 502, mirroring the slotting engine'sslotting-engine-unavailablesemantics (ADR 0017): the caller's request was fine, the upstream was not.nonestays 503, and providernonenever produces a model error. - There is no retrieval over an embedding store: no vector extension, no embedding table and no document index.
- No operational path calls the copilot. Every warehouse decision is made by deterministic code.
Consequences
- A deployment that does not want AI has none. The default is off, and nothing else in the product
calls a model. Registration in the runtime store would be a silent no-op - the provider is
resolved once at startup - so the AI knobs are the documented exemption in
the-configuration-boundary.md, environment-only like the other tier-3 knobs. - The copilot knows only what its tools return, read from live data at the moment of the question.
- With
anthropicoropenrouter, the conversation and every tool result it draws on are sent to that provider's API; this is stated in the user guide and in the README. Withollama, they go to the Ollama server named byOLLAMA_URLand stay on the machine that runs it. - At the 2.0.0 release the copilot is optional and provider-dependent: everything is unit- and
integration-tested with a mocked model, the deterministic E2E proves the disabled state, and
the live operational check (
tests/e2e/operational/copilot-live.operational.ts) is written to fail on a provider that does not answer. It has run green against OpenRouter with its default DeepSeek V4 Flash model (deepseek/deepseek-v4-flash-0731) on 2026-09-27, recorded in docs/release-evidence; no provider key is available to the repository, and none is committed, so the other two providers (Anthropic and Ollama) remain unverified live. Release notes must describe the copilot with that verified-provider sentence, never as generally working. - All three provider extensions are in every image. The Anthropic and OpenRouter keys have
non-empty placeholder defaults, because the extensions validate them at startup even when the
provider is
none(SRCFG00040 boot-trap rule). - Questions about documents that are not in the database - procedures, manuals - cannot be answered; nothing indexes them.
- Why a third provider was added, why the copilot ships switched off, and why two hosted tiers exist are recorded in the decision and in the runtime configuration guide.
Alternatives considered
- Retrieval over a vector store in PostgreSQL, feeding stored documents to the model. Not built. It answers a different question - what do our documents say - from the one the copilot answers - what is true in the warehouse now. The RAG alternative is not built because the copilot answers the warehouse question, not the document question.
- Automatic fallback from the hosted provider to the local one. Not built; the provider is chosen by configuration, and a silent fallback would hide a misconfigured hosted provider.
- An LLM framework in another language, run as a separate service. Rejected: a second runtime,
a second build and a network hop, where LangChain4j runs in the same JVM and joins Quarkus's
dependency injection (
@RegisterAiService,@Tool). - Calling a provider's API directly, with no framework. Rejected: the tool-calling protocol, conversation memory and the abstraction over providers would all have to be written and kept.
- Another hosted model family, or a direct DeepSeek API. OpenRouter was chosen over a direct
second vendor for the same reason Karyo chose an abstraction over providers: one key and one
OpenAI-compatible endpoint cover many model families, and operators can point the same named
model at any other compatible service later. The model itself is configuration
(
KARYO_AI_ANTHROPIC_MODEL,KARYO_AI_OPENROUTER_MODEL,KARYO_AI_OLLAMA_MODEL). - Why a self-hosted provider is offered beside the hosted one: a warehouse that must not let any conversation leave its network can run Ollama on its own hardware for the same panel; the hosted tiers are for warehouses that accept the egress in exchange for a managed model.
Evidence
services/karyo-app/src/main/resources/application.yaml,quarkus.langchain4j- three named models, fixed at build time, each with explicit timeout and token cap:quarkus.langchain4j.anthropic.claude.*- model-nameclaude-opus-5-5(wasclaude-opus-4-20250514, retired by Anthropic 2026-06-15), timeout 30 s, max-tokens 4096quarkus.langchain4j.openai.openrouter.*- base-urlhttps://openrouter.ai/api/v1, model-namedeepseek/deepseek-v4-flash-0731, timeout 30 s, max-tokens 4096quarkus.langchain4j.ollama.local.*- base-urlOLLAMA_URL, model-idllama3.1, timeout 30 s, num-predict 4096max-retries: 0on the Anthropic chat model and the OpenRouter model (application.yaml,quarkus.langchain4j.anthropic.claude.chat-model.max-retries, quarkus.langchain4j.openai.openrouter.max-retries)
infrastructure/docker/nginx/nginx.conf,location /api/v1/ai/-/api/v1/ai/proxied with a 120 s read timeout- OpenRouter model catalog,
https://openrouter.ai/api/v1/models(fetched 2026-09-25):deepseek/deepseek-v4-flash-0731is a live id ("DeepSeek: DeepSeek V4 Flash 0731") advertisingtoolsandtool_choice; no batch or free variant exists for that id. - Anthropic model documentation and deprecations (fetched 2026-09-25):
claude-opus-4-20250514retired 2026-06-15;claude-opus-5-5Active, no retirement before 2027-09-22. libs/karyo-common/src/main/kotlin/com/karyo/common/exception/ProblemDetail.kt- the RFC 7807 body;services/ai-service/karyo-ai-core/src/main/kotlin/com/karyo/ai/exception/CopilotExceptionMapper.kt- the 502 mapping, mirroring
SlottingExceptionMapper.
- the 502 mapping, mirroring
services/ai-service/karyo-ai-core/src/main/kotlin/com/karyo/ai/service/CopilotService.kt,CopilotService.memoryKey- the tenant-bound memory key;
CopilotService.chatandCopilotService.forgetIdle- memory across turns, forgotten when idle;CopilotService.recordOutcome- a proposal's outcome recorded in its owner's memory;.../dto/ChatRequest.kt- the input bounds.
- the tenant-bound memory key;
services/ai-service/karyo-ai-core/src/main/kotlin/com/karyo/ai/api/v1/CopilotResource.kt,CopilotResource- chat, every proposal this reply's tools prepared, and 503 when off.
karyo-app/.../application.yaml,quarkus.langchain4j.anthropic.claude.api-key, quarkus.langchain4j.openai.openrouter.api-key- the placeholder keys;karyo.ai.provider- the provider switch,noneby defaultservices/ai-service/karyo-ai-core/src/main/kotlin/com/karyo/ai/config/AiConfig.kt,AiConfig.../ai/service/ClaudeCopilot.kt,ClaudeCopilot,.../ai/service/OpenRouterCopilot.kt,OpenRouterCopilotand.../ai/service/LocalCopilot.kt,LocalCopilot- one set of tools, three models.../ai/tools/WarehouseActionTools.kt,WarehouseActionTools- the write role checked, a proposal stored.../ai/tools/WarehouseInsightTools.kt,WarehouseInsightTools- recent activity, from the outboxservices/ai-service/karyo-ai-core/src/main/kotlin/com/karyo/ai/api/v1/CopilotResource.kt,CopilotResource- confirm and decline: ownership, then role, then execute or discard, then the outcome recordedservices/ai-service/karyo-ai-core/build.gradle.kts,dependencies- LangChain4j and all three providers, and no vector store- Events and the outbox