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

# Resume session

> Resume previous Laravel Copilot SDK sessions using listSessions(), fixed session IDs, ResumeSessionConfig, and last-session helpers.

## Resume session

How to resume a previous session.

## When using interactive chat in an Artisan command

```php theme={null}
// Get previous sessions with listSessions
$sessions = Copilot::client()->listSessions();

// Choose a session ID to resume with Laravel\Prompts\select
$sessions = collect(Copilot::client()->listSessions())
    ->mapWithKeys(function (SessionMetadata $session) {
        return [$session->sessionId => $session->summary ?? ''];
    })
    ->toArray();

$session_id = select(
    label: 'What session do you want to resume?',
    options: $sessions,
);

// Resume the session with the selected ID
$session = Copilot::client()->resumeSession($session_id);

// Get previous messages
$messages = $session->getMessages();
```

## When using a fixed session ID

If your flow is not interactive, specify a fixed ID from the beginning.
Create IDs based on user IDs or other context.
For example, this works well in a multi-user web service.

```php theme={null}
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;

$config = new SessionConfig(
    sessionId: 'user-123-conversation',
);

Copilot::start(function (CopilotSession $session) {
    dump('Starting Copilot session: '.$session->id());

    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config);
```

When resuming, you can also pass `resume` to `Copilot::start`.

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;
use Revolution\Copilot\Types\ResumeSessionConfig;

// ResumeSessionConfig does not allow setting sessionId
$config = new ResumeSessionConfig();

Copilot::start(function (CopilotSession $session) {
    dump('Starting Copilot session: '.$session->id());

    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config, resume: 'user-123-conversation');
```

You can also discard the current session inside the `Copilot::start` closure and resume another one.

```php theme={null}
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;

Copilot::start(function (CopilotSession $session) {
    dump('Starting Copilot session: '.$session->id());

    $session->disconnect();

    $session = Copilot::client()->resumeSession(sessionId: 'user-123-conversation');

    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
});
```

## Resume the last session

If no session exists, `getLastSessionId()` returns `null`, and the session starts as new.
You can switch between `SessionConfig` and `ResumeSessionConfig`.

```php theme={null}
use Revolution\Copilot\Types\SessionConfig;
use Revolution\Copilot\Types\ResumeSessionConfig;
use Revolution\Copilot\Contracts\CopilotSession;
use Revolution\Copilot\Facades\Copilot;

$session_id = Copilot::client()->getLastSessionId();

if (empty($session_id)) {
    $config = new SessionConfig();
} else {
    $config = new ResumeSessionConfig();
}

Copilot::start(function (CopilotSession $session) {
    dump('Starting Copilot session: '.$session->id());

    $response = $session->sendAndWait(prompt: 'Tell me something about Laravel.');
}, config: $config, resume: $session_id);
```

## Use `SessionMetadata`

`getSessionMetadata()` also returns `null` when the session does not exist, so you can use the same new-session fallback.
It also provides metadata such as created/updated timestamps.

```php theme={null}
use Revolution\Copilot\Types\SessionMetadata;
use Revolution\Copilot\Facades\Copilot;

$meta = Copilot::client()->getSessionMetadata('user-123-conversation');
```

<Info>
  For the latest updates, see the [GitHub repository](https://github.com/invokable/laravel-copilot-sdk).
</Info>


## Related topics

- [SessionConfig](/en/packages/laravel-copilot-sdk/session-config.md)
- [Agent loop](/en/packages/laravel-copilot-sdk/agent-loop.md)
- [Plugin Directories](/en/packages/laravel-copilot-sdk/plugin-directories.md)
- [Session hooks](/en/packages/laravel-copilot-sdk/hooks.md)
- [Telemetry](/en/packages/laravel-copilot-sdk/telemetry.md)
