> ## Documentation Index
> Fetch the complete documentation index at: https://www.sapientpriors.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# A team knowledge base

> An app where users build knowledge bases from files and chats, share them with colleagues, and ask them questions from anywhere.

## Example use case

Your users create knowledge bases for a project, a research area, or a team. They feed each one documents and selected chats, share it with colleagues, and ask it questions, either in your app or from inside another assistant that uses it as context.

## Set up

* **One project** for the app, with a **service account** that holds the Project admin role, so your code can create agents and delete files and memories. See [Set up your organization](https://www.sapientpriors.com/docs/walkthroughs/set-up-your-organization.md).
* **One agent per knowledge base.** Memory never crosses agents, so two knowledge bases never mix. A user can own several.
* **Your app decides who opens which knowledge base.** Store each knowledge base's `agent_id` and its members on your server.
* Send `X-SP-End-User` with the member's id on every call made for that member. Each member's conversations are then their own threads on the shared agent.

Create the agent when a user creates a knowledge base, then poll [GET`/v2/agents/{agent_id}`](https://www.sapientpriors.com/docs/api-reference/v2/agents/get-agent) until `status` is `ready`:

```json theme={null}
{
  "agent_config": {
    "role": "Pricing research knowledge base for Northwind",
    "objective": "Remember the documents, decisions, and updates shared here, who shared them, and when, so every member gets accurate answers about pricing research."
  }
}
```

> [!WARNING]
>
> **Everything one member adds to a shared knowledge base can be answered back to every member.** Say so in your app when a user shares a knowledge base or adds content to one.

## Flow 1: add files

Upload files with multipart [POST`.../observe`](https://www.sapientpriors.com/docs/api-reference/v2/threads/learn-messages-or-files), with one `file` part per document and the `speaker` who uploaded them. Use one uploads thread per knowledge base, or one per member.

* An agent reads one file at a time. Further uploads queue and report `queued`. Poll each observation until `committed` before you tell the user the file is ready.
* Uploading the same file twice to one agent returns `409 document_already_ingested`. It is already learned.
* Answers cite the uploaded files, with a download link.
* To replace a document, delete the old file with [DELETE`/v2/agents/{agent_id}/files/{file_id}`](https://www.sapientpriors.com/docs/api-reference/v2/files/delete-file), wait until its memories are forgotten, then upload the new version.

Formats and size limits are in [Files](https://www.sapientpriors.com/docs/concepts/files.md).

## Flow 2: add chats

Users choose which of their chats feed which knowledge base. Each chat becomes its own thread on the agent, so recent context never mixes between chats. Send its messages with `observe`, oldest first, each with `speaker`, `speaker_id`, and the original `timestamp`.

* **Turn on for a chat:** send the chat's history, then each new message as it happens.
* **Share when done:** send the whole chat in one go.
* **Send one message:** a single message or exchange.

If you include an assistant's replies, give them their own `speaker`, so the knowledge base can tell what a person said from what a model wrote. Send an `Idempotency-Key` on each request, and wait for `committed` before you send the next batch to the same agent.

## Flow 3: answer questions

| Where | Call | What happens |
| - | - | - |
| In your app | [POST`.../observe-and-respond`](https://www.sapientpriors.com/docs/api-reference/v2/threads/learn-and-answer) with `stream: true` | Priors writes the answer and learns the question. |
| A one-off question that must not be learned | [POST`.../respond`](https://www.sapientpriors.com/docs/api-reference/v2/threads/answer) | Priors writes the answer. Nothing is learned. |
| Inside another assistant, as context | [POST`.../recall`](https://www.sapientpriors.com/docs/api-reference/v2/threads/recall-memories), or [POST`.../observe-and-recall`](https://www.sapientpriors.com/docs/api-reference/v2/threads/learn-and-recall) to also learn | Priors returns the relevant dated memories. Your assistant's model writes the answer. |

With a learning call, the question itself is learned. In a shared knowledge base, every member's questions then feed the shared memory.

## Flow 4: remove content

* **A document:** [DELETE`.../files/{file_id}`](https://www.sapientpriors.com/docs/api-reference/v2/files/delete-file) removes the file and every memory made from it.
* **Something a member said:** list memories with [GET`/v2/agents/{agent_id}/memories`](https://www.sapientpriors.com/docs/api-reference/v2/memories/list-memories) filtered by `thread_id` or `speaker_id`, then delete the turn with [DELETE`.../memories/{turn_id}`](https://www.sapientpriors.com/docs/api-reference/v2/memories/forget-a-memory).
* **A whole knowledge base:** [DELETE`/v2/agents/{agent_id}`](https://www.sapientpriors.com/docs/api-reference/v2/agents/delete-agent). It cannot be undone.

## What goes wrong

> [!WARNING]
>
> **Your server decides who opens which knowledge base.** One service-account key reaches every public agent in its project. Never accept an `agent_id` from the client without checking it against the member list on your server. Without that check, one user can read another's knowledge base by changing an id.

* **`X-SP-End-User` treated as a memory boundary:** it separates thread lists and rate-limit windows, not memory. Use separate agents for content that must stay apart.
* **The key in a browser or app bundle:** anyone who loads the page can read it. Call Priors from your server only.

## Calls used

* Guides: [Service accounts](https://www.sapientpriors.com/docs/access/service-accounts.md), [Files](https://www.sapientpriors.com/docs/concepts/files.md), [Answering](https://www.sapientpriors.com/docs/concepts/answering.md), [Memories](https://www.sapientpriors.com/docs/concepts/memories.md)
* API reference: [Create agent](https://www.sapientpriors.com/docs/api-reference/v2/agents/create-agent.md), [Learn messages or a file](https://www.sapientpriors.com/docs/api-reference/v2/threads/learn-messages-or-files.md), [Learn and answer](https://www.sapientpriors.com/docs/api-reference/v2/threads/learn-and-answer.md), [Recall memories](https://www.sapientpriors.com/docs/api-reference/v2/threads/recall-memories.md), [Delete file](https://www.sapientpriors.com/docs/api-reference/v2/files/delete-file.md), [Forget a memory](https://www.sapientpriors.com/docs/api-reference/v2/memories/forget-a-memory.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.