---
title: "Survive an expired session · Agent Wranglers"
url: https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures/session-expiry
updated: 2026-10-20
licence: "All rights reserved"
---

# Survive an expired session

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Permissions, flags and failures](https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures)
- Rule: When a session expires, keep the screen and the unsaved work. Let the person sign in again in place, then repeat the request that failed.
- How: For script requests, the server answers 401 with problem details, never a redirect to an HTML sign-in page. The client keeps its state, opens sign-in in a dialog or a new tab, and retries the request once sign-in succeeds. Warn at least 2 minutes before a timed sign-out and offer more time, as [time limits](https://agentwranglers.se/playbook/website/accessibility/time-limits) requires.
- Check: A browser test fills a form, deletes the session cookie and submits. A sign-in prompt appears, and after signing in the form is saved with the values typed before.

Example:

```ts
export async function apiFetch(input: string, init?: RequestInit): Promise<Response> {
  const response = await fetch(input, init);
  if (response.status !== 401) return response;
  await signInAgain();
  return fetch(input, init);
}
```

```python
@app.errorhandler(SessionExpired)
def session_expired(_error):
    wants_page = request.accept_mimetypes.best == "text/html" and "HX-Request" not in request.headers
    if wants_page:
        return redirect(url_for("sign_in", next=request.path))
    return problem(401, "session-expired", title="Please sign in again")
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers app interface playbook: https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures/session-expiry`.

- Previous: [Let the server say what each user may do](https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures/capabilities-from-server)
- Next: [Read feature flags before the first paint](https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures/feature-flags-in-ui)
- [Permissions, flags and failures](https://agentwranglers.se/playbook/app-ui/permissions-flags-and-failures)
