chatbot architecture

Rendered from the public repository's docs. Design decisions → · Code on GitHub

Current state of the chatbot service. Update this file in the same change as any code change that alters components, flows, limits or data (CI checks that every path mentioned here exists).

Context

admin

Visitor

Reverse proxy

chatbot service

LLM API

PostgreSQL

Owner

Visitors reach the service only through a reverse proxy that terminates TLS and routes requests; the service itself binds to loopback only (ADR-0001). The owner reads stored conversations through a bearer-token admin endpoint.

Components

PathResponsibility
cmd/server/main.goWiring (config → store → llm → server), graceful shutdown, healthcheck subcommand for the container
internal/config/config.goEnvironment variables and defaults; secrets read from *_FILE paths
internal/server/server.goHTTP routes, CORS allow-list, request log, panic recovery
internal/server/ratelimit.goPer-IP token bucket
internal/server/gapfilter.goStrips the leading [GAP] marker from the streamed answer
internal/llm/llm.goLLM client behind an interface: Allowed() guard, Stream() answer
internal/llm/prompts.goGuard prompt, answer prompt, refusal text
internal/knowledge/knowledge.goEmbedded knowledge text and [[MISSING: ...]] parsing
internal/store/store.gopgx pool, embedded migrations, insert/history/count/list
DockerfileMulti-stage build, distroless, non-root

Chat request lifecycle

LLM (answer)LLM (guard)PostgreSQLServerBrowseralt[cap reached]alt[BLOCK or guard error (fail closed)][ALLOW]POST /api/chatvalidate body, per-IPlimit, concurrency slotdaily count, last N turns(server-side history)error eventis this about the owner?canned refusalstream answer grounded in knowledge texttokensSSE delta events, then donestore exchange (status, tokens,latency, hashed IP)

Data

MESSAGES

bigint

id

PK

uuid

session_id

timestamptz

created_at

text

ip_hash

text

question

text

answer

text

status

bool

knowledge_gap

text

model

int

input_tokens

int

output_tokens

int

latency_ms

One table, messages: one row per exchange. A conversation is the set of rows sharing a session_id (no separate table); the server loads the last N rows of a session as history, so clients never supply it. status is answered, refused or error; knowledge_gap marks questions the bot could not answer; ip_hash is an HMAC, never a raw address. Indexes serve history lookups (session_id, created_at), recent-first listing, and a partial index on knowledge gaps. Migrations are embedded in the binary and applied on boot under an advisory lock, so concurrent starts are safe (ADR-0004).

Limits (defaults in internal/config/config.go)

500-character message, 10 turns of history, per-IP rate limit, daily exchange cap, output-token cap, concurrency cap. All are configurable; production values are set outside the code.

Security and privacy

Testing strategy

llm.Client and the store are interfaces; tests use fakes, so go test -race ./... needs no network or API key (ADR-0003).