# Sessions and Context

Pythinker Code automatically saves your conversation history, allowing you to continue previous work at any time.

## Session resuming

Each time you start Pythinker Code, a new session is created. While running, you can also enter the `/new` command to create and switch to a new session at any time, without exiting the program.

If you want to continue a previous conversation, there are several ways:

**Continue the most recent session**

Use the `--continue` flag to continue the most recent session in the current working directory:

```sh
pythinker --continue
```

**Interactively pick a session**

Use `--session` (or `--resume`, `-S`, `-r`) without an argument to open an interactive session picker, where you can use arrow keys to select the session to resume:

```sh
pythinker --session
```

> The interactive picker is only available in shell mode.

**Resume a specific session**

Use `--session` (or `--resume`) with a session ID to resume that specific session:

```sh
pythinker -r abc123
```

If the specified session ID does not exist, a new session is created automatically.

**Switch sessions during runtime**

Enter `/sessions` (or `/resume`) to view all sessions in the current working directory, and use arrow keys to select the session you want to switch to:

```
/sessions
```

The list shows each session's title and last update time, helping you find the conversation you want to continue. Press `Ctrl-A` to toggle between showing sessions for the current directory only or across all directories, making it easy to find sessions across projects. Use `/title <text>` to set a custom title for the session, making it easier to find later.

**Resume hint on exit**

When a session exits — whether through normal exit, `Ctrl-C` interruption, `/undo`, `/fork`, `/sessions` switch, or other scenarios — Pythinker Code automatically prints a resume command hint:

```
To resume this session: pythinker -r <session-id>
```

You can copy this command directly and run it in your terminal next time to quickly resume the session. Empty sessions do not show this hint.

**Startup replay**

When you continue an existing session, Pythinker Code will replay the previous conversation history so you can quickly understand the context. During replay, previous messages and AI responses will be displayed.

## Session state persistence

In addition to conversation history, Pythinker Code also automatically saves and restores the session's runtime state. When you resume a session, the following states are automatically restored:

- **Approval decisions**: YOLO and auto mode on/off status, operation types approved via "allow for this session"
- **Plan mode**: Plan mode on/off status
- **Subagent instances**: Subagent instance state and context history created via the `Agent` tool during the session
- **Additional directories**: Workspace directories added via `--add-dir` or `/add-dir`

This means you don't need to reconfigure these settings each time you resume a session. For example, if you approved auto-execution of certain shell commands in your previous session, those approvals remain in effect after resuming.

## Export and import

Pythinker Code supports exporting session context to a file, or importing context from external files and other sessions.

**Export a session**

Enter `/export` to export the current session's complete conversation history as a Markdown file:

```
/export
```

The exported file includes session metadata, a conversation overview, and the complete conversation organized by turns. You can also specify an output path:

```
/export ~/exports/my-session.md
```

**Import context**

Enter `/import` to import context from a file or another session. The imported content is appended as reference information to the current session:

```
/import ./previous-session-export.md
/import abc12345
```

Common text-based file formats are supported (Markdown, source code, configuration files, etc.). You can also pass a session ID to import the complete conversation history from that session.

::: tip
Exported files may contain sensitive information (such as code snippets, file paths, etc.). Please review before sharing.
:::

## Clear and compact

As the conversation progresses, the context grows longer. Pythinker Code will automatically compress the context when needed to ensure the conversation can continue.

You can also manually manage the context using slash commands:

**Clear context**

Enter `/clear` to clear all context in the current session and start a fresh conversation:

```
/clear
```

After clearing, the AI will forget all previous conversation content. You usually don't need to use this command; for new tasks, starting a new session is a better choice.

**Compact context**

Enter `/compact` to have the AI summarize the current conversation and replace the original context with the summary:

```
/compact
```

You can also append custom instructions after the command to tell the AI what content to prioritize preserving during compaction:

```
/compact keep the database-related discussion
```

Compacting preserves key information while reducing token consumption. This is useful when the conversation is long but you still want to retain some context.

::: tip
The bottom status bar displays the current context usage with token counts and a fill bar (e.g., `ctx 36k/200k ████▌░░░░░ 18%`), helping you understand when you need to clear or compact.
:::

::: tip
`/clear` and `/reset` clear the conversation context but do not reset session state (such as approval decisions, dynamic subagents, and additional directories). To start completely fresh, it's recommended to create a new session.
:::
