Skip to content

[Feature Request] Add session ID retrieval command for interactive sessions #1407

Closed

Description

@NatiCha

Currently, there's no straightforward way to retrieve a session ID from an active Claude Code session, which severely limits the utility of the claude -r "<session-id>" resume command. Users need the session ID to resume conversations later, but have no way to obtain it during an interactive session.

Problem: Without access to the session ID, the resume functionality becomes nearly impossible to use effectively, especially for long-running projects where you might want to return to a specific session days or weeks later.

Proposed Solutions:

  1. Add session ID to interactive commands: Introduce a new command (e.g., /session) or extend the existing /status command to display the current session ID during an active Claude Code session.
  2. Enhance the session list view: Include the session ID as a column in the claude -r table output, making it easy to copy and reference for future use.

These changes would make session management much more practical and allow developers to effectively pause and resume their work across multiple sessions.

Related to #1335

Activity

  1. JoshHeitzman commented on Jun 9, 2025

    @JoshHeitzman

    +1 for adding the session id to /status

    I found this trying to figure out how to get the session ID after Claude Code - Opus started wildly underperforming after some server overloaded error messages. I think the server side context got reset, without that having been made obvious on the client. So now I'm trying to figure out how to recover from that situation in the future. Same for when the local claude process crashes while compacting its context.

  2. gwpl commented on Jun 15, 2025

    @gwpl

    Related to the session identification challenges discussed here, I've created #2112 proposing a --session-name feature that would allow users to assign custom names to sessions.

    This would complement the session ID retrieval functionality by making sessions human-readable and easier to identify in the --resume picker.

    Other related issues addressing session management:

  3. bcherny commented on Jun 18, 2025

    @bcherny
    Collaborator

    Shipping in tomorrow's release!

  4. timmfin commented on Jun 20, 2025

    @timmfin

    Thanks @bcherny, though in the latest version I see this when I do /status:

     Claude Code Status v1.0.30
    
      L Session ID: 764c8585-3aba-4adb-bf16-...
    

    ... which does give some way to get at the session id manually. And while that is helpful, but still is limiting because only (from what I can see) only a real user can get at the session ID†.

    What I'm looking and hoping for is a way to make custom commands (or manually provided prompts) get at the session ID so that we can "agentically" chain one claude execution to another. For example, have one claude process, kicked off in interactive mode or with -p do some work and then paste a command to the user (or automatically run) which will resume after some long wait.

    So now I can manually copy the session id, close the current instance of claude code, and then write a command like:

    ./wait-until-build-finishes.sh && claude --resume <copied session id> -p "Build finished, use MCP to get status. If it failed try and fix and push the changes. If it succeeded merge the pr with gh cli"
    

    ... but I can't (very easily) do something like:

    claude -p "/pr-and-write-session-id-to-file /tmp/work1.txt" && ./wait-for-build && claude --resume $(cat /tmp/work1.txt) --p "/merge-or-try-and-fix-build"
    
    # Where /pr-and-write-session-id-to-file and /merge-or-try-and-fix-build are custom slash commands I've created
    

    ps: I know there is the streaming output, but that is tricky to work with, especially when you want to using interactive mode most of the time. Though maybe there is a way to write a slash command that writes to look at the ~/.claude/projects/<project>/ folder to see if it can find the session id 🤔

    †: Here was may attempt to "agentically" get at the session ID (that didn't succeed):

    
    > Get the current session Id and write it to /tmp/test.txt
    
    ⏺ I'll get the current session ID and write it to /tmp/test.txt.
    
    ⏺ Bash(echo $ANTHROPIC_SESSION_ID > /tmp/test.txt)
      ⎿  (No content)
    
    ⏺ Read(../../../../../tmp/test.txt)
      ⎿  Read 2 lines (ctrl+r to expand)
    
    ⏺ The session ID appears to be empty. Let me check what environment variables are available and
      try a different approach:
    
    ⏺ Bash(env | grep -i session || echo "No session variables found")
      ⎿  SECURITYSESSIONID=YYY      
         WARP_IS_LOCAL_SHELL_SESSION=1
    
    ⏺ Bash(echo "Session ID not available in environment variables" > /tmp/test.txt)
      ⎿  No (tell Claude what to do differently)
    
    > Use the status slash command to get the current session id
    
    ⏺ Bash(/status)
      ⎿  Error: (eval):1: no such file or directory: /status
    
    ⏺ Let me try the status command differently:
    
    ⏺ Bash(status)
      ⎿  Error: (eval):1: command not found: status
    
    ⏺ It seems the status command isn't available as a shell command. The session ID might need to
      be obtained through a different method, or it may not be accessible in this environment. Would
       you like me to try a different approach to get session information?
    
  5. ogt commented on Jun 21, 2025

    @ogt

    This is what I am/was using to get to the session id :-) .

    ------------
    > I want you to find the current session id. One idea is to look in ~/.claude and look for a unique characteristic of this 
      session. For exampe call the uuidgen program - find its output and then look for session jsonl (in the corresponding 
      ~/.claude/projects/<encoded_cwd>/.. whose tail|grep has that string...
    
    ⏺ I'll help you find the current session ID by generating a unique identifier and then searching for it in the Claude session
       files.
    
    ⏺ Bash(uuidgen)
      ⎿  FC2638E0-D228-4DE2-8CF1-7668EE726523
    
    ⏺ Now let me search for session files in the ~/.claude directory:
    ........
    ⏺ Found it! The current session ID is 69420655-04d6-4eb4-87e6-9f62741fb8cd.
    .......
    ⏺ The current session ID is 69420655-04d6-4eb4-87e6-9f62741fb8cd
    ------------
    and from the newly released /status
     Claude Code Status v1.0.31
      L Session ID: 69420655-04d6-4eb4-87e6-9f62741fb8cd
    
    

    I have similar use case as @timmfin - not sure how I could use the /status command . I am recording session ids with PRs locally so as when things comes back (from jenkins/PR reviewer comments/ sentry errors post release - ) I can go from PR=> session id and launch an interactive session --resume the original one with additional pasted [user] input whatever are the complaints and have claude code give it first a try - and if it can finish up by itself again the ci/cd. Still , the info is up there in .claude/ - so with some hack like what I am describing above... its not difficult to get the session id.

    PS. @bcherny you are awesome 🤩

  6. ogt commented on Jun 23, 2025

    @ogt

    In case anyone finds this usefull this is how my scripts currently work:

    ithaca:test odysseas$ alias myclaude='claude "please run uuidgen and then save_claude_vars --add_session_by_unique_string <uuidgen output>. Both save_claude_vars and uuidgen are in your path"'
    thaca:test odysseas$ myclaude
    
    ╭───────────────────────────────────────────────────╮
    │ ✻ Welcome to Claude Code!                         │
    │                                                   │
    │   /help for help, /status for your current setup  │
    │                                                   │
    │   cwd: /Users/odysseas/test                       │
    ╰───────────────────────────────────────────────────╯
    
    
     ※ Tip: Did you know you can drag and drop image files into your terminal?
    
    > please run uuidgen and then save_claude_vars --add_session_by_unique_string <uuidgen output>. Both
       save_claude_vars and uuidgen are in your path
    
    ⏺ I'll run uuidgen to generate a UUID and then save it using save_claude_vars.
    
    ⏺ Bash(uuidgen)
      ⎿  FC7DCA21-1676-42D2-A3CC-AD25249902F0
    
    ⏺ Bash(save_claude_vars --add_session_by_unique_string FC7DCA21-1676-42D2-A3CC-AD25249902F0)
      ⎿  Session info saved for: 29f62c4c-9590-485e-8d8e-6c641f90592d
           Claude PID: 57860
           Terminal: 2B7F2A7E-C963-40C4-840E-7E5631853568
         … +2 lines (ctrl+r to expand)
    
    ⏺ Done. Session saved with UUID FC7DCA21-1676-42D2-A3CC-AD25249902F0.
    
    > /exit 
    
    
    ithaca:test odysseas$ grep -rl '18DC93BA-1E29-4888-83D9-2641ABE8F3C2' ~/.claude/recorded_sessions/
    /Users/odysseas/.claude/recorded_sessions/9458d1f0-d62e-43a2-9606-cb479b037f5d.jsonl
    ithaca:test odysseas$ 
    
    

    save_claude_vars

  7. colllin commented on Jul 10, 2025

    @colllin

    It looks like a (volatile) workaround might be to just read the ~/.claude/projects/-workspace/*.jsonl files after starting a new Claude session, and assume the newest file is the new session ID.

    It also looks like resuming a session is considered a new session with a new session ID (more like a fork), so if you want to resume the resumed session, you need the new session ID.

    $ claude -p "Hello"
    Hello! I'm here to help you with your video messaging application project. What would you like to work on today?
    
    $ ls -t ~/.claude/projects/-workspace | head -n 1
    27900125-3315-4d0a-a049-e6a8fd6ae4e2.jsonl
    
    $ claude -r 27900125-3315-4d0a-a049-e6a8fd6ae4e2 -p "What was my previous message, verbatim?"
    Your previous message was:
    
    "Hello"
    
    $ ls -t ~/.claude/projects/-workspace | head -n 1
    55ace50a-73f9-4cc0-8ed0-d2f2523288b8.jsonl # <= NOTE: new session ID 
    
    $ claude -r 55ace50a-73f9-4cc0-8ed0-d2f2523288b8
    > Hello
    
    ● Hello! I'm ready to help you with your video messaging application project. What would you like to work on today?
    
    > What was my previous message, verbatim?
    
    ● Your previous message was:
    
      "Hello"
    
    > 
    
    $ claude -r 27900125-3315-4d0a-a049-e6a8fd6ae4e2
    > Hello
    
    ● Hello! I'm ready to help you with your video messaging application project. What would you like to work on today?
    
    > 
    
  8. kym6464 commented on Jul 13, 2025

    @kym6464

    I built a little tool to solve the "session retrieval" problem for myself: https://github.com/kym6464/claude-code-tools

  9. gregor-RW commented on Jul 18, 2025

    @gregor-RW

    Copying from another issue => #1335

    Running --output-format json we get:

    {
      "type": "result",
      "subtype": "success",
      "is_error": false,
      "duration_ms": 7899,
      "duration_api_ms": 7865,
      "num_turns": 1,
      "result": "Looking at ... Cloude Response ...",
      "session_id": "f38b6614-d740-4441-a123-0bb3bea0d6a9",
      "total_cost_usd": 0.08348025,
      "usage": {
        "input_tokens": 3,
        "cache_creation_input_tokens": 21379,
        "cache_read_input_tokens": 0,
        "output_tokens": 220,
        "server_tool_use": {
          "web_search_requests": 0
        },
        "service_tier": "standard"
      }
    }```
    
    and the session_id is right there!
    
  10. github-actions commented on Jul 30, 2025

    @github-actions

    This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.

  11. locked as resolved and limited conversation to collaborators on Jul 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions