ReplyBrain Docs LLMs

Facts

Facts are served by your app at POST /replybrain/facts/<topic>, signed by ReplyBrain. Read Facts and account scope first for when they are read.

The request and the answer

{ "ref": { "type": "order", "id": "4521", "account_scope": "42" }, "purpose": "support", "query": { "around": "2026-11-12T09:30:00Z" } }
{
  "facts": { "reference": "A-4521", "status_label": "Shipped", "eta": "2026-11-14" },
  "revision": "2026-11-12T08:01:13.000000Z",
  "observed_at": "2026-11-12T09:30:02.114000Z",
  "ref": { "type": "order", "id": "4521" }
}
Answer Meaning
200 with facts and observed_at Read. Both are required; without them the answer is unreadable.
404 not_found You serve the topic, but the record is not there, or not this account's.
404 unsupported (or 405, 501) You serve no such topic.

The gem answers all of this for you.

The account topic

The account topic (default user) is read with the account_scope as its id, so declare it on the model whose id is your account scope:

class User < ApplicationRecord
  replybrain_facts(:user, account: :id) do |user, _account_scope, query|
    around = query["around"]&.then { Time.iso8601(_1) }
    {
      name: user.first_name,
      plan_label: user.plan.display_name,
      exports: { recent: user.exports.near(around).limit(5).map { |e| { id: e.id, reference: e.number, state_label: e.state.humanize } } }
    }
  end
end

query["around"] is when the customer wrote, so a list can hold the records of those days (the order they mention from last month), not only the newest.

Record topics

Records the customer's message may be about: declare them in c.record_topics and serve each one. ReplyBrain finds them in the account topic's lists, <plural>: { recent: [...] } or <topic>_candidates: [...], each item with an id and optionally the reference customers quote. It reads up to three cited records per draft.

class Export < ApplicationRecord
  replybrain_facts(:export, account: :user_id) do |export|
    { reference: export.number, state_label: export.state.humanize, finished_at: export.finished_at }
  end
end

# Not an Active Record model:
ReplyBrain::Client.facts(:shipment, find: ->(id) { Shipments.lookup(id) }, account: :customer_id) { |s| { carrier: s.carrier } }

Rules that keep drafts right

WARNING

Declare account: on every topic. Without it, every account-scoped request answers not_found: the gem fails closed. With it, a record of another account answers exactly like a missing one.

TIP

Labels, not codes. A draft repeats what your facts say. Send status_label: "Waiting for payment" beside (or instead of) status: 3; a customer must never read an internal code.

TIP

Keep it minimal. Return what a support reply needs, nothing else: no password hashes, tokens, full card numbers, internal notes or other customers' data. Everything you return may be quoted to this customer.

  • Each answer holds for one hour from its observed_at, and is read again right before a draft is placed. Make the read fast: all of a draft's reads share a budget of about ten seconds.
  • revision is the record's updated_at; a change between the first read and the placement stops the draft and a new one is written.
  • Every topic listed in the manifest must be served, or the manifest is refused.