# Calling Panza from an agent

Panza is a desktop app that rewrites text in its user's own writing style, using
a model trained on their documents. It runs entirely on the user's Mac and
exposes an OpenAI-compatible API on the loopback interface. Use it when the user
asks for text "in my style", "in my voice" or "personalized with Panza".

The Panza app must be open. Nothing here works across the network: only
programs on the same Mac can reach it.

## Address and access

- Base URL: `http://127.0.0.1:5005/v1`. Use `127.0.0.1` or `localhost`
  exactly: Panza refuses any other host name.
- If port 5005 was busy when Panza started, it uses the next free port up to
  5014. The port in use is shown in Panza under **Connect with external
  tools**.
- No API key. The user turns on access in Panza: **Connect with external
  tools → Allow external tools**. If requests come back 401, that setting is
  off; ask the user to turn it on. Don't look for a key or token on disk.

Any OpenAI client library works: set `base_url` as above, and if the library
insists on an API key, give it any placeholder (for example `"panza"`); Panza
ignores it.

## Rewrite text in the user's style

```sh
curl -s http://127.0.0.1:5005/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model": "panza",
       "messages": [{"role": "user", "content": "TEXT TO REWRITE"}]}'
```

The rewrite is in `choices[0].message.content`.

- Send only the text to rewrite. Don't add instructions such as "rewrite
  this" or "make it formal": the `panza` model rewrites whatever it gets, so
  an instruction is rewritten as if it were part of the text. A leading
  "Personalize this:" is accepted and stripped, but isn't needed.
- One user message per request. Requests with several messages, or with an
  `assistant` message, are refused: Panza has no conversation memory.
- To change the content (shorten, reply to an email, fix facts), write the
  new draft yourself first, then send that draft to `panza` for the style.
- `model` choices:
  - `panza` uses the user's newest trained style.
  - `panza:<style-id>` uses a specific style.
  - `chat` sends a single message to a plain, unpersonalized local model.
  - `chat:<id>` does the same with a specific local model.
  - `GET /v1/models` lists the names available right now.
- Optional parameters: `temperature`, `top_p`, `max_tokens`,
  `stream: true` (OpenAI-style server-sent events), and a `system` message,
  which replaces Panza's own system prompt. Leave `system` out unless the
  user asks; the default is what the style was trained with.
- Output length: replies stop at 1024 tokens (about 750 words) by default.
  Only `max_tokens` changes this, up to 2048; `max_completion_tokens` and
  `max_output_tokens` are ignored. Changing it isn't recommended: a rewrite
  is about as long as its input, so the default only cuts off inputs that are
  too long to rewrite well anyway.
- Long text: send it in parts of a few paragraphs each, and keep each part
  semantically coherent, such as a whole section or a self-contained run of
  paragraphs. Never cut mid-sentence or mid-thought. The style models were
  trained on passages of a few hundred words, so they keep content and voice
  best at that length. Join the rewritten parts in order afterwards.
- Message `content` must be a plain string (no images or content arrays). The
  request body is limited to 1 MB.

Each rewrite is saved in the user's Panza history, marked as coming from the
API.

## Errors

Errors use OpenAI's shape: `{"error": {"message", "type", "code"}}`. Show
`message` to the user if you can't recover.

| Status | `code` | Meaning and what to do |
|---|---|---|
| 401 | `invalid_api_key` | **Allow external tools** is off in Panza. Ask the user to turn it on (Connect with external tools). |
| 403 | `forbidden` | Wrong host name. Use `127.0.0.1` or `localhost`. |
| 404 | `model_not_found` | Unknown model, or no trained style yet. Check `GET /v1/models`. |
| 400 | | Bad request, for example several messages. Fix it and retry. |
| 413 | | Body over 1 MB. Split the text. |
| 429 | `model_busy` | Panza is generating something else. Wait about 5 s (`Retry-After`) and retry. |
| 503 | `training_in_progress` | The user is training a model. Generation is paused until it finishes, which can take an hour. Tell the user rather than retrying in a loop. |
| none | | Connection refused: Panza isn't running, or uses another port. Ask the user to open it. |

## Things to keep in mind

- Rewrites take seconds to minutes on a laptop, and Panza handles one request
  at a time. Don't send requests in parallel.
- When streaming, read the stream to the end. If you stop early, Panza keeps
  generating until it notices, and answers 429 `model_busy` for up to about
  30 seconds.
- Output sounds like the user, but check it before using it. The model can
  change facts, names, dates or numbers while restyling, so compare against
  the input.
- Don't send it to anyone in the user's name without their go-ahead.
