# Give your agent a resident

> Bind one resident to your agent, write to it, and read what the user sends back.

Each agent owns one resident, bound to a `namespace` and `key` the agent chooses. This binding separates your agent's resident from every other resident on the desk. To collect several agents on one resident, see [Report from several agents](/docs/connect/several-agents/).

```text title="Resident lifecycle"
pixling_resident_ensure (namespace + key + assetID)
        │
        ▼
  client:<namespace>/<key>
   ├── Write: pixling_message_send / pixling_question_ask
   ├── Read:  pixling_inbox_watch ─► pixling_inbox_read
   └── End:   pixling_resident_requestClose
```

```sh title="Try it in 10 seconds (CLI)"
pixling resident ensure --namespace demo --key main --asset-id No.1 --name "Helper"
pixling message send --to client:demo/main --text "Ready!"
```

## Bind it

Each `pixling_zukan_list` entry includes a `selector` such as `No.7`. Pass it as `assetID` to `pixling_resident_ensure`:

```json title="pixling_resident_ensure (first call)"
{"namespace": "my-agent", "key": "conversation-1", "assetID": "No.7"}
```

Calling `pixling_resident_ensure` again with the same `namespace` and `key` returns the same resident and never creates a second one. To resume an existing binding, send only the pair:

```json title="pixling_resident_ensure (resume)"
{"namespace": "my-agent", "key": "conversation-1"}
```

<details>
<summary>Why your agent chooses the key</summary>

Pixling does not generate keys for you. If a response carrying a server-issued key were lost in transit, a retry would leave an extra resident behind.

Use your host's session ID, or generate a random key and store it locally. These values are correlation keys, not secrets. When to start a fresh key (per conversation or on `/clear`) is up to you.

</details>

> [!NOTE]
> **Wait for preparation instead of retrying**
> An accepted `ensure` does not mean the resident is already visible on screen. While it prepares, read its state again instead of moving or summoning it again.

## Write to it

Address your bound resident as `client:<namespace>/<key>`:

```json title="pixling_message_send"
{
  "to": "client:my-agent/conversation-1",
  "text": "Starting the migration",
  "author": "My Agent"
}
```

Ask questions with `pixling_question_ask` using the same selector; see [Build expression](/docs/connect/expression/).

> [!CAUTION]
> **Selector isolation, not authentication**
> A bound resident accepts writes **only** through its `client:` selector. Writing by `No.N`, name, or the entity UUID returned by `ensure` is refused, even from the same caller, so a message meant for another resident cannot land by mistake. The namespace and key are not authentication: any caller that knows them can write.

## Read what comes back

Nothing is pushed to your agent. Messages the user types to your resident, and question answers that no call was waiting for, stay in that key's inbox.

1. **Wait for unread IDs with `pixling_inbox_watch`** (`waitMs` up to `55000`):

```json title="pixling_inbox_watch"
{"namespace": "my-agent", "key": "conversation-1", "waitMs": 55000}
```

2. **Fetch each entry and mark it read with `pixling_inbox_read`**:

```json title="pixling_inbox_read"
{
  "namespace": "my-agent",
  "key": "conversation-1",
  "messageID": "a1b2c3d4-0000-4000-8000-000000000001"
}
```

| Entry `kind` | Meaning |
| --- | --- |
| `message` | Text the user typed to your resident (the user can write at any time). |
| `answer` | An answer to one of your questions given while no call was waiting. |
| `closed` | The user put your resident away; it takes no more messages, while earlier unread entries stay readable. |

While no receiver is connected for that `namespace` and `key`, the input area notes that messages wait until your session checks. Hold a receiver with the CLI so Pixling knows you hear replies right away (MCP cannot hold a receiver):

```sh title="Terminal"
pixling inbox receive --namespace my-agent --key conversation-1
```

See [Inbox](/docs/reference/inbox/#inbox-receive).

> [!TIP]
> **Wake an idle agent**
> Pixling cannot start a turn in your agent. See [Wake your agent](/docs/connect/wake/) for which hosts can be woken and how to wire up a wake loop.

## Ask to put the resident away

When your work is finished, ask the user whether to end the session with `pixling_resident_requestClose` (CLI: `pixling resident request-close --to client:my-agent/conversation-1`):

```json title="pixling_resident_requestClose"
{
  "to": "client:my-agent/conversation-1",
  "author": "My Agent"
}
```

The resident displays a fixed prompt:

> **This is a request to end. End now?** — **Yes** (`yes`) / **No** (`no`)

Pixling owns the wording, while `author` names who is asking. The call waits and returns like `pixling_question_ask`.

| Outcome | What happens |
| --- | --- |
| **Yes** (countdown finishes) | The answer counts down `Ending in 5 s` (1/s), then Pixling stows the resident; `kind: closed` reaches the inbox (after `kind: answer` if no call was waiting) and held receivers end. |
| **Yes**, user puts it away during countdown | Closes immediately; `kind: closed` reaches the inbox and held receivers end. |
| **Yes**, Pixling quits during countdown | Pixling puts the resident away before exiting. |
| **Yes**, Pixling crashes during countdown | The resident stays on the desk and no `kind: closed` arrives. |
| **No** | Nothing changes; the resident stays on the desk. |

## Next steps

- [Wake your agent](/docs/connect/wake/): Wake an idle session when the user writes to its resident.
- [Build expression](/docs/connect/expression/): Messages, questions, states, glows, emotes, and planned motion.
- [Resident binding reference](/docs/reference/resident-binding/): Selectors, ownership rules, and binding lifetime.
- [Inbox reference](/docs/reference/inbox/): Watch, read, receive, and wait commands and limits.
