Your agent needs a /cd command — here's how to build directory-aware sessions
Claude Code's new /cd command is brilliant. It lets you move your session to a new directory without blowing your prompt cache. But here's the thing — your AI agent needs this pattern even more than you do.
Most coding agents die when they need to work across multiple services or repositories. They start in /api, get context about the backend, then you ask them to fix something in /frontend and they completely lose the thread. The context switch kills them.
I've been running a coding agent that handles our entire monorepo for three months. Here's the session wrapper that makes it work:
#!/bin/bash
# session-manager.sh
SESSION_ROOT="/workspace"
CONTEXT_DIR="$SESSION_ROOT/.agent-context"
cd_with_context() {
local target_dir="$1"
# Save current context
pwd > "$CONTEXT_DIR/last_dir"
ls -la > "$CONTEXT_DIR/last_listing"
# Move to new directory
cd "$target_dir" || return 1
# Load new context
echo "Now in: $(pwd)"
echo "Key files:"
find . -maxdepth 2 -name "*.md" -o -name "package.json" -o -name "requirements.txt"
# Update agent memory
echo "$(date): Moved to $target_dir" >> "$CONTEXT_DIR/session_log"
}But the real magic isn't the bash script — it's teaching your agent to maintain directory-aware context. Your agent needs to know:
- Where it is — current working directory, not just conceptually
- Where it came from — the last directory and why it was there
- What's here — key files, configs, and dependencies in the new location
- How this connects — relationship between current and previous directories
Here's the agent prompt pattern that makes this work:
When changing directories: 1. Run `pwd` to confirm current location 2. List key files with `find . -maxdepth 2 -type f -name "*.json" -o -name "*.md" -o -name "*.py"` 3. Check for README, package.json, or other context files 4. Update your working memory with: - Previous directory: [where you came from] - Current directory: [where you are now] - Key files found: [list] - Connection: [how this relates to previous work] 5. Before making changes, confirm you understand the codebase structure
Pro tip: Create a .agent-context directory in your project root with README files for each major directory. Your agent will automatically discover these and understand the codebase structure.
The difference is night and day. Instead of starting over every time it changes directories, your agent maintains continuity. It remembers that the API change it just made needs a corresponding frontend update. It knows that the database migration it's writing affects both the backend models and the admin interface.
This isn't just about coding agents. Any agent that works with files — content creation, data analysis, documentation — needs directory awareness. The pattern scales from simple file organization to complex multi-service architectures.
The key insight: your agent's working directory is part of its context, not just a technical detail. Treat directory changes like context switches and give your agent the tools to handle them gracefully.