Skip to main content
The Company Brain replaces knowledge bases. KnowledgeBases keeps working (see Migrating from knowledge bases), but new code should use Brain. For the product side, see the Company Brain guide.
The Brain module is the entry point to the Company Brain: ask a question and open what it cites, manage collections and their documents, specific brains, what each agent reads, the learned facts agents propose, and the connected sources. Every method has an async form (a prefix) and a sync form.

Which key works

The brain answers for one person: the owner of the API key. Every ask, read and change runs with that person’s access, so they see only the documents and sources they may read.

Constructor

When base_url is not given, the SDK resolves it in this order:
  1. The XPANDER_BRAIN_BASE_URL environment variable.
  2. Derived from configuration.base_url:
    • https://inbound.<domain> (the SDK default) becomes https://api.<domain>/v1.
    • An agent-controller URL is kept, with the organization appended (inside a deployment).
    • Any other URL is kept as is.
The brain’s routes are then <base>/brain/....

Module methods

ask

Brain.aask asks the brain a question as the key’s owner.

Parameters

Returns BrainAskResult

Citation fields: id (pass it to read), title, source_kind, source_label, url_or_ref, page, para, onsite.

Sync version

read

Brain.aread opens one cited item in full: a citation id from an ask, or a source and its reference.

Parameters

Raises ValueError when neither a citation id nor both source and ref are given.

Returns BrainReadResult

status, ok, citation, text, message, as for ask.

Sync version

Collections

A collection holds on-site documents. Its audience says who may read it, with access tokens org:*, user:<id>, group:<id> or agent:<id>.
Left out on create, audience makes a member’s collection theirs alone and an admin’s org wide. Only admins can give a collection to someone else or share it org wide (unless the settings allow members to). Collection fields: id, name, description, owner_id, owner_name, project_id, audience, documents (count), brains (ids of brains holding it), members_can_add_files, can_manage, can_add_files, version.

Documents

BrainDocument fields: id, collection_id, title, owner_id, owner_name, access (effective tokens), version, current_version_id, status (queued, parsing, ready, failed), error, step, pages, sections, review_by, acl (set by get_document and update_document), and the helpers is_ready / is_failed. A document the key’s owner may not read answers 404.

upload_document

Brain.aupload_document adds a document to a collection. Give exactly one of file_path, content (with filename), text or url.

Parameters

Files are sent inline, 25 MB at most (ValueError above that, or when not exactly one content is given).

Returns DocumentIngestResult

document_id, version_id, version and status (queued). Parsing and embedding run in the background.

wait_until_ready

Brain.await_until_ready polls a document until its newest version is ready.
Returns the ready BrainDocument. Raises ModuleException 422 when ingestion failed (with the reason) and 408 when it is still running after timeout. A 502, 503 or 504 while polling is retried until the deadline.

Versions and chunks

Brains

The Company Brain holds everything the person asking can read; it keeps its name and cannot be deleted (409). A specific brain holds the collections and source slices you give it.
reads_as="user" reads as the person asking; "agent" reads as the agent it is attached to (see Reads as the user, or as the agent). BrainItem fields: id, name, description, is_company, reads_as, owner_id, owner_name, project_id, contents (BrainContentItem: id, kind, ref, name, scope, documents, sensitive), agent_count, agents_off (Company Brain only), version, can_edit.

What an agent reads

Each agent has a Company Brain toggle, specific brains and collections attached straight to it. These calls save right away: no agent.save() is needed (unlike attach_knowledge_base). From the Brain client:
aset_agent_brains(agent_id, *, company=None, brains=None, collections=None): None keeps the current value; a list replaces it. At most 50 brains and collections together. From an Agent:
AgentBrains fields: company, brains, collections, public_agent. The key’s owner must be able to see the agent, and changing what it reads follows the agent’s edit rights (403 otherwise).

Learned facts

Agents propose facts they learned; their owner reviews them before anyone’s answers use them. See Learned facts.
BrainFact fields: id, text, previous_text, kind (fact, decision, procedure), status, proposed_by_agent, proposed_by_agent_id, run_id, proposed_at, audience, target_collection, conflicts_with, conflicts, owner_id, decided_by, decided_at.

Sources

Read only: the on-site knowledge and the connected systems the brain reads live as each person.

The caller and settings

aupdate_settings takes members_can_create_collections, members_can_share_org_wide and agents_can_propose_facts; None keeps a setting.

Migrating from knowledge bases

KnowledgeBases and KnowledgeBase keep their signatures and keep working. Constructing KnowledgeBases emits a DeprecationWarning once per process. Knowledge bases that moved into a brain are served by the brain behind the same calls; new content should go to collections. Differences to know:
  • ask searches with the key owner’s access (and, through brain=, one brain at a time); a knowledge base search read whatever the KB held.
  • ask returns cited passages with titles, pages and source links instead of {content, score} rows; use read to open a cited item in full.
  • Documents have versions, an owner, a review date and their own access list.

Errors

Every method raises ModuleException on failure (ask and read do not raise for a non-ok status): Invalid arguments caught before the request (no content, two contents, a file over 25 MB, no read target) raise ValueError.