> ## 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.

# Optimize your agents with Claude Code

> The full loop for a coding agent: analyse, propose, test before applying, apply, verify, roll back

Ask Claude Code (or Codex, or another coding agent) to "optimize my agents", and it can run the whole improvement loop with the `totebot` CLI: find what is wrong, propose changes with evidence, test them without saving, apply only what you approve, check the results, and roll back if they got worse. This page is the human-readable version of "Recipe: optimize my agents" in `totebot ai-guide`, the Claude Code skill and `AGENTS.md`, which all hold the same steps.

The same checks power [AI Lab](https://docs.totebot.ai/dashboard/ai-lab.md) in the dashboard. For the dashboard version of the loop, see [Optimize your agent](https://docs.totebot.ai/dashboard/optimize-your-agent.md).

## Set up

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

Follow [Totebot CLI](https://docs.totebot.ai/cli/overview.md), then teach Claude Code the CLI as in [Use the CLI with coding agents](https://docs.totebot.ai/cli/coding-agents.md).

**2. Choose what the key may do**

Start read-only. The agent can then analyse and propose, and nothing changes. Turn on write scopes under **Team settings → API keys → Edit access** only when you want it to apply changes.

| To | Scopes |
| - | - |
| Analyse | `agents:read`, `knowledge:read`, `conversations:read`, `analytics:read`, `events:read` (default), plus `quality:read` (opt-in) for test questions |
| Check the instructions | `quality:write` (the check runs a model) |
| Test a proposal | `quality:write` for test runs; `chat:write` and `agents:write` or `quality:write` for preview chats |
| Apply | `agents:write` for settings, `knowledge:write` for knowledge |
| Live experiment (optional) | `experiments:write` and `agents:write`, and live experiments turned on for your team |
| See provider cost in USD | `billing:read` (opt-in). Credits are always shown. |

**3. Ask**

For example: *"Optimize the Shop agent. Show me what you would change and why before you change anything."*

## The recipe

### 1. Analyse

```bash theme={null}
totebot report <agent> --timezone Europe/Zagreb --json
totebot analyse outcomes <agent> --interval week --timezone Europe/Zagreb --json
totebot usage show --by model --json
totebot agents readiness <agent> --json
totebot knowledge list <agent> --status FAILED --json
totebot knowledge list <agent> --stale-before 2026-10-01 --json
totebot events --types handover.requested,knowledge.failed,test_run.finished --json
totebot agents revisions <agent> --json
```

* `report` is one overview: outcomes, revenue, credits, knowledge, the test pass rate and the top gaps. Use `--all-agents` for the whole team.
* `analyse outcomes` gives per week the handover rate, thumbs-up rate, how often the agent answered without a search, empty searches, ticket resolution time, conversion rate and revenue. The agent looks for the weeks that got worse.
* `agents readiness` is the checklist on the AI Lab Findings tab: failed and expired knowledge, knowledge no search returned in 30 days, open gaps, the test pass rate, whether "Check knowledge before every answer" is on, and model advice (credit multiplier, search rate, thumbs-down rate and credits per conversation per model).
* `events` and `agents revisions` show what happened lately and which settings changed.
* `knowledge list` shows each source's `validFrom` / `validUntil`. An expired promotion is fixed with `totebot knowledge set-dates <agent> <source> --valid-until <date>` if the offer still runs, or deleted if it is over. A new time-limited offer gets `--valid-until` too, so the agent stops using it on time.

Then the agent follows [Analyse your agent with Claude Code](https://docs.totebot.ai/api/analyse-with-claude-code.md) steps 1 to 4 for gaps, empty searches and a few bad conversations.

### 2. Check the instructions

```bash theme={null}
totebot agents check-prompt <agent> --json
```

An AI review of the instructions. Each finding has a severity, a category (conflict, belongs in knowledge, missing basics, language and tone, length and cost, outcomes), an explanation and an exact edit. `suggestedPrompt` has every edit applied: it is a suggestion to show you, never something to save on its own. Findings marked `requiresHumanReview` add links, contact details, discounts or offers, and are applied only with your explicit OK. Results are cached for 24 hours per prompt, and there is a daily limit.

### 3. Propose

The agent writes a short list of concrete changes, each with its evidence: Q\&A or text to add with exact wording, sources to retry, remove or re-date, instruction edits as a diff, a model change with credits per conversation before and after, and settings. It checks its own draft too, and makes sure there are test questions for what it changes:

```bash theme={null}
totebot agents check-prompt <agent> --prompt-file proposed.md --json
totebot tests export <agent> > tests.json
totebot tests import <agent> tests.json --dry-run --json
totebot tests import <agent> tests.json --json
```

Then it asks you which changes to apply.

### 4. Test before applying

```bash theme={null}
totebot agents get <agent> --json > before.json
totebot tests run <agent> --prompt-file proposed.md --model <model id> --fail-under <current pass rate> --json
totebot chat <agent> "Do you deliver on Saturdays?" --prompt-file proposed.md --model <model id> --json
```

* `before.json` keeps the live instructions and model id.
* `tests run` with `--prompt-file` or `--model` runs your test questions against the proposal without saving it, and compares it with the live agent. Exit code 10 means the pass rate fell below the threshold; exit code 11 means the test run itself failed.
* `chat` asks one question with the proposal and returns the reply and the steps behind it. No conversation is created, customers never see it, and it is billed like a dashboard preview (200 per key per day).
* Optional: `totebot experiments start <agent> --name "Shorter prompt" --percent 20 --prompt-file proposed.md --yes --json` sends 20% of new conversations to the proposal (see [Experiments](https://docs.totebot.ai/dashboard/experiments.md)). Experiments are turned on per workspace: contact support to enable them. Real customers see it, so the agent runs it only after the test run passed and you agreed; without a terminal it needs `--yes`.

### 5. Apply

Only what you approved, with a preview first:

```bash theme={null}
totebot agents update <agent> --prompt-file proposed.md --model <model id> --dry-run --json
totebot agents update <agent> --prompt-file proposed.md --model <model id> --yes --json
totebot train start <agent> --wait --json
```

Leave out `--model` when only the instructions change, and `--prompt-file` when only the model changes, so you apply exactly what you tested.

The dry run prints `changes`, each field `before` and `after`. Without a terminal, `agents update` needs `--yes`; the agent passes it only after you approved that change. Train after knowledge changes. Every write is recorded in the audit log under the key's name and kept as a revision.

### 6. Verify

```bash theme={null}
totebot tests run <agent> --fail-under <pass rate before> --json
```

After a few days of traffic, the agent runs `agents readiness`, compares `analyse outcomes` for the weeks before and after, follows `totebot events --follow --json`, and for an experiment reads `totebot experiments report <agent> <experiment> --json` (thumbs-down rate, handover rate, no-search rate and credits per conversation for CONTROL and CANDIDATE). Small samples prove little, and it says so.

### 7. Roll back if it got worse

```bash theme={null}
totebot experiments stop <agent> <experiment> --yes --json
totebot agents revisions <agent> --json
totebot agents diff <agent> <revision>
totebot agents rollback <agent> <revision> --dry-run --json
totebot agents rollback <agent> <revision> --yes --json
```

`experiments stop` ends an experiment. For a saved change, `agents rollback` puts back the settings from just before that revision: pick the revision of the change you made, not simply the newest, in case something else changed since. The dry run prints `changes` as `{ field: { from, to } }` and `skippedFields`: settings a rollback leaves alone, such as AI features only Totebot support changes, auto-retrain and gap digest emails, and anything your plan no longer includes. The rollback is a revision too, so it can be undone. Revisions cover settings, not knowledge: knowledge changes are undone with `knowledge update` or `knowledge delete`, then training.

## In CI

Keep the instructions and test questions in git, and fail the pipeline when a change breaks the answers that matter:

```bash theme={null}
totebot tests import <agent> tests.json --json
totebot tests run <agent> --prompt-file prompt.md --fail-under 90 --format junit > junit.xml
```

Set `TOTEBOT_API_KEY` in the CI secrets, with only the scopes the job needs. See [Test questions](https://docs.totebot.ai/dashboard/test-questions.md#use-test-questions-as-a-gate).


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