Per-session scratch directory environment variable for temporary files

Resolved 💬 4 comments Opened Apr 9, 2026 by daira Closed Jun 11, 2026

Background

When Claude is working through a long task, it often needs to write temporary files (build logs, intermediate captures, debug dumps, scratch scripts, etc.). The natural place is /tmp, but /tmp is shared with:

  • Other concurrent Claude Code sessions
  • Other tools and processes running on the same machine
  • Leftover debris from earlier runs of the same or different tools

This causes two related problems:

  1. Collisions: hardcoded names like /tmp/build.log can clash between concurrent sessions, with one session's output overwriting another's, or one session reading another's stale file.
  1. False positives in searches: Claude sometimes needs to find files generated during the task. Broad searches like find /tmp -name foo -mmin -5 can match files belonging to other sessions or tools, leading to confused diagnostics — Claude sees a /tmp/test*/ directory and assumes it belongs to the current task, but it's actually a leftover from another tool.

Proposal

Expose a per-session scratch directory to Claude via an environment variable, e.g. CLAUDE_SESSION_TMPDIR, that:

  • Is created at session start
  • Is unique per Claude session (not per task, not per tool call)
  • Is cleaned up at session end (or on a documented retention policy)
  • Is exported into the environment of every Bash tool invocation
  • Is visible to Claude so it can reason about it (e.g., mentioned in system context, or readable via a known mechanism)

This way Claude can write \$CLAUDE_SESSION_TMPDIR/build.log instead of /tmp/build.log, and searches scoped to \$CLAUDE_SESSION_TMPDIR are guaranteed not to match unrelated activity.

Bonus: optionally also set TMPDIR=\$CLAUDE_SESSION_TMPDIR in the Bash tool environment, so child processes that honor TMPDIR (e.g., mktemp, many test frameworks) automatically end up writing into the session-scoped directory.

Why a built-in is better than just doing it in-session

Claude can already do this manually: at the start of a session, run \mktemp -d /tmp/claude-session-XXXXXX\, capture the path, and use it for all temp files. But this has problems:

  • Claude has to remember to do it consistently in every session (easy to forget)
  • The path has to be remembered across many tool calls (relies on context window staying in scope)
  • Subagents and child processes started by tools won't automatically use it
  • No automatic cleanup at session end

A built-in feature would address all of these.

Related observations

Claude Code already manages per-session directories internally (for things like task output capture). So a session identifier exists internally; exposing it (or a scratch path derived from it) to user code would be a small extension.

---

🤖 This issue was filed with the assistance of Claude Opus 4.6 (1M context).

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗