#14182·paperclip

Anthropic subscription AI connections expire after ~8h: only the access token is stored, env-injected, never refreshed

Author: alvarobolanosCreated Sep 26, 2026Updated Sep 28, 2026

Summary

An Anthropic subscription AI connection stops working ~8 hours after it is connected. Every run then fails with acpx_turn_failed / ACP agent reported a terminal access failure. until a human clicks Connect again. Reconnecting fixes it for another ~8 hours, so it looks intermittent, but it is deterministic: Paperclip stores a short-lived OAuth access token for Anthropic with no way to refresh it.

Root cause (traced in 2026.916.1)

  1. Only the access token is captured. server/dist/services/local-ai-credentials.js → readVerifiedLocalAiCredential("anthropic") reads claudeAiOauth.accessToken from the host's ~/.claude/.credentials.json and returns just that string. The refresh token in the same file is never stored.

  2. For Anthropic the token is env-injected, not file-backed. server/dist/services/ai-connection-runtime.js:

    javascript
    const subscriptionFile = selection.attribution.method === "subscription" &&
        input.binding.provider !== "anthropic";
    ...
    if (subscriptionFile) await writeFile(authFile, value, { mode: 0o600 });
    else env[capability.envKey] = value;   // anthropic → CLAUDE_CODE_OAUTH_TOKEN

    (envKey: "CLAUDE_CODE_OAUTH_TOKEN" in shared/dist/ai-connections.js.)

  3. The refresh/rotate path can't apply. The post-run logic that re-reads authFile and rotates the secret (ai-connection-runtime.js ~L222–260, errorLabel: "AI account refresh") only works for the file-backed providers. A static CLAUDE_CODE_OAUTH_TOKEN cannot be refreshed by Claude Code, so nothing ever rotates for Anthropic.

  4. Failure surfaces as an "access" session failure. The spawned Claude process gets an auth rejection and reports _meta.jetbrains.air.sessionFailure.category = "access", which acpx turns into ACP agent reported a terminal access failure. (prompt-turn in acpx/dist/live-checkpoint-*.js). The run record shows errorCode: acpx_turn_failed, and after the pre-flight starts failing, configuration_incomplete / reason: "ai_connection_unavailable" with nextAction: "Reconnect the selected AI account…".

Net effect: Anthropic subscription connections silently expire on the access-token TTL. Agents with no runtimeConfig.aiConnection binding don't hit this because they use the host's live ~/.claude login, which self-refreshes — but once a binding exists it can't be removed through the API (aiConnection is .optional(), not nullable, and PATCH /agents/:id refills it when omitted), so users can't opt back out.

Reproduce

  1. Sign in to Claude Code on the host (subscription account).
  2. In Paperclip, connect Anthropic → Claude subscription ("Claude Code is signed in. Click Connect to use this account.") and bind an agent to it (responsible_user or shared).
  3. Run tasks — they work.
  4. Wait for the access token TTL (~8h; the host's ~/.claude/.credentials.json expiresAt shows when).
  5. Every run now fails with ACP agent reported a terminal access failure. Click Connect again → works again for ~8h.

Observed: 5/5 runs on one agent failed identically over ~10h, then succeeded immediately after POST /companies/:id/ai-connections/local with the existing connectionId (re-snapshot), with no other change.

Expected

Either store the refresh token as well and refresh before/after runs (as the file-backed providers effectively get), or give the Anthropic subscription flow a file-backed CLAUDE_CONFIG_DIR so Claude Code can refresh it itself, or at minimum surface "token expired — reconnect" instead of a generic access failure.

Workaround in use

A systemd user timer re-runs the local reconnect every 4h: POST /api/companies/<companyId>/ai-connections/local with {provider:"anthropic", method:"subscription", ownership:"personal", connectionId:"<existing>"}. It works because readVerifiedLocalAiCredential re-reads the host's (self-refreshing) login and verifies it with fetchClaudeQuota before storing.

Related

While debugging, PATCH /agents/:id with any change to an agent that has an aiConnection binding consistently returned 422 The selected AI connection failed validation in this agent’s environment (the validateManagedAgentBinding(..., test=true) hello-probe with engine: "cli"), even while normal persistent-session runs on the same connection succeeded. Happy to split that into its own issue if useful.

Environment

  • Paperclip 2026.916.1 (server, adapter-claude-local, shared, adapter-utils all 2026.916.1), embedded Postgres, local_trusted mode
  • Claude Code 2.1.273, Claude Pro subscription
  • Linux (Arch/Omarchy), agents on claude_local, mode: persistent