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

# Analyse your agent with Claude Code

> Let a coding agent find what your knowledge is missing, fix it and check the fix with test questions

A coding agent such as Claude Code can read how your agent answers customers: which knowledge each reply used, which searches found nothing, which sources are never used, and which questions keep coming back as knowledge gaps. With a read-only key it reports and recommends. With write access it can also add the fixes, retrain and run your test questions to compare before and after.

## Set up

**1. Create a key**

In **Team settings** → **API keys**, create a key. The default read access (`knowledge:read`, `conversations:read`) covers answers, searches, stats and gaps; also turn on `quality:read` to read test questions and runs. To let the agent apply fixes, also turn on `knowledge:write` and `quality:write`.

`customers:read` is off by default, so the analysis endpoints return customer emails and phone numbers as `[email]` and `[phone]` and keep them out of the agent's context.

**2. Install the CLI and sign in**

```bash theme={null}
npm i -g @totebot/cli
totebot login
```

Or set `TOTEBOT_API_KEY` in the environment. See [the CLI overview](https://docs.totebot.ai/cli/overview.md).

**3. Ask**

In Claude Code, ask for example: *"Analyse the knowledge base of my Shop agent and tell me what to fix."* The Totebot skill and `totebot ai-guide` give the agent a step-by-step recipe.

## What the agent does

The recipe in `totebot ai-guide` ("Analyse and improve my knowledge base") walks through these steps:

| Step | Command | API |
| - | - | - |
| 1. Snapshot | `totebot analyse snapshot <agent>` | `GET /v1/agents/{agentId}/analysis-snapshot` |
| 2. Stats and empty searches | `totebot analyse stats <agent>`, `totebot analyse retrieval <agent> --empty` | `GET …/answer-stats`, `GET …/retrieval-events?empty=true` |
| 3. Gaps | `totebot gaps list <agent>` | `GET …/gaps` |
| 4. Bad conversations | `totebot analyse answers <agent> <conversation>` | `GET …/conversations/{conversationId}/answers` |
| 5. Recommendations | a report for you, with evidence | |
| 6. Apply Q\&A fixes (write) | `totebot gaps resolve`, `totebot knowledge add-qa`, `totebot train start --wait` | `POST …/knowledge/suggestions/{id}/resolve`, `POST …/training` |
| 7. Test (write) | `totebot tests add`, `totebot tests run --wait` | `POST …/test-questions`, `POST …/test-runs` |
| 8. Compare | `totebot tests results <agent> <run>` | `GET …/test-runs/{runId}` |

The API reports data as it is (counts, sources, scores, labels). The analysing agent decides what matters.

To go further than knowledge, with instruction and model changes tested before they are applied and a rollback if results get worse, see [Optimize your agents with Claude Code](https://docs.totebot.ai/cli/optimize-agents.md).

## What the data means

* **Answers.** For each customer message: the replies, every knowledge search with the sources it returned and their scores, the tools the bot called, and `usedNoTool` when the bot answered from its instructions alone. Tool calls come from the stored playground trace when there is one, otherwise from the conversation state (`toolCallsSource`).
* **Searches.** `origin` is `TOOL` (the bot searched), `PREFETCH` (the automatic search before every answer) or `PRODUCT_FINDER`. `empty: true` means nothing came back.
* **Stats.** `retrievalShare` is the share of customer messages with at least one search. `neverReturnedSources` lists live sources no search returned in the range: often outdated, duplicated, or worded differently from how customers ask.
* **Gaps.** `verification` is `MISSING` (not in the knowledge) or `PARTIAL` (partly covered). `RETRIEVAL_MISS` gaps, hidden by default, are covered by the knowledge but search did not find the answer: rewording or a Q\&A usually fixes them. `signals` shows thumbs-down and handovers. `suggestedAnswerDraft` comes from a team member's reply, with emails and phone numbers masked.
* **Test questions.** Each has the sources search must return and the facts the answer must state. A run reports pass or fail per question. Add `--prompt-file` to `totebot tests run` to try new instructions without saving them.

Searches are kept for 90 days. For older ranges `retrievalDataAvailable` is `false` and the search metrics are `null`, so they read as not available rather than 0%. Playground conversations are left out of the stats.

## What the agent never sees

* Other teams' data, or agents the key is not allowed to access.
* System notes, raw tool output, visitor ids, IP addresses or message metadata.
* Customer emails and phone numbers, when the key has no `customers:read`.

## Example session

```text theme={null}
> Analyse the knowledge of my Shop agent and fix what you can.

● totebot agents list --json
● totebot analyse snapshot agt_shop --json
  412 sources (3 failed: 404 on two links, one PDF too large). 27 pending gaps
  (19 MISSING, 8 PARTIAL). Last 30 days: 1,240 customer messages, 81% with a
  search, 14% of searches empty. Last test run: 31/36 passed.
● totebot analyse retrieval agt_shop --empty --limit 100 --json
  Empty searches cluster around "return label", "Click & Collect" and "gift
  wrapping".
● totebot gaps list agt_shop --json
  Top gaps: "How do I print a return label?" (MISSING, 14 conversations,
  thumbs-down and handover), "Can I collect in store?" (PARTIAL, 9).
● totebot analyse answers agt_shop cnv_8f2 --json
  The bot searched "return label" twice, both empty, then answered without
  knowledge and the customer asked for a person.

Findings
1. Returns: the knowledge explains the 30-day policy but not how to get a label.
2. Click & Collect: the only source is a 2024 page; opening hours are missing.
3. 38 sources were never returned, 22 of them old blog posts.
Proposed fixes: 2 Q&A pairs (wording below), retry the 3 failed sources.
Apply them?

> Yes.

● totebot gaps resolve agt_shop gap_41 --question "How do I print a return label?" --answer "..." --json
● totebot gaps resolve agt_shop gap_52 --question "Can I collect my order in store?" --answer "..." --json
● totebot knowledge retry-failed agt_shop --json
● totebot train start agt_shop --wait --json
● totebot tests add agt_shop --question "How do I get a return label?" --fact "Labels are in My orders → Return" --json
● totebot tests run agt_shop --wait --json
  33/37 passed (was 31/36). Still failing: 4 questions about gift wrapping,
  missing fact "gift wrapping costs 3 EUR". Want me to add that?
```

## Plain API

Without the CLI, the same calls are in the typed client (`totebot.analysis`, `totebot.gaps`, `totebot.tests`) and in the [API reference](https://docs.totebot.ai/api/public-api.md#analysis):

```ts theme={null}
const snapshot = await totebot.analysis.snapshot(agentId);
const empty = await totebot.analysis.retrievalEvents(agentId, { empty: true });
const gaps = await totebot.gaps.list(agentId);
const run = await totebot.tests.startRun(agentId);
const result = await totebot.tests.waitForRun(agentId, run.id);
```


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