With several Claude Code sessions running in the background, a terminal screen is a poor way to keep track. The same list is available as JSON, which suits a script better.
claude agents opens agent view, a screen for monitoring and dispatching parallel background sessions. With --json it prints the active sessions as a JSON array instead, so no interactive terminal is needed.
claude agents --json
Add --all to include completed background sessions. --cwd limits the list to sessions started under one directory:
claude agents --json --all --cwd ~/projects/example
Pipe it to jq to pick what you need. The reference does not list the fields of each entry, so I do not either. Print the output once and read the keys before you build on them.
claude agents --json | jq 'length'
That counts the active sessions, a quick check for a status bar or a cron job. To jump into one of them from the terminal, claude attach <id|name> attaches to a background session, and claude --bg "query" starts one and returns right away.
Opening agent view itself requires an interactive terminal. Only the JSON output is meant for scripts.
Further reading: Claude Code CLI reference