Chapter 5: Claude Code Setup
You've built the roads. Now it's time to put the driver behind the wheel.
Meet Your New Research Assistant
Claude Code is not ChatGPT in a terminal. It's not a chatbot you ask questions to and copy-paste answers from. It is an autonomous agent that lives inside your terminal and has direct access to your file system. It can read files. It can write files. It can run shell commands. It can SSH into your GPU servers. It can edit your training scripts, fix bugs, sync code, launch experiments, and monitor their progress — all without you lifting a finger.
When you talk to Claude Code, you're not having a conversation with a language model. You're giving instructions to an agent that will go and do things. You say "check if the training on server b2 is still running." It doesn't give you a generic answer about how to check tmux sessions. It actually SSHes into b2, runs tmux ls, reads the output, and tells you the answer. You say "the learning rate is too high, change it to 1e-4 and restart." It opens the config file, edits the value, syncs the code to the server, kills the old process, and starts a new one.
This is what makes it different from every other AI tool you've used. It doesn't just think — it acts. And because it acts within your terminal, it has the same access you do. Your files. Your servers. Your conda environments. Your git repositories. Everything.
The previous four chapters built the infrastructure that makes this possible. SSH gives Claude Code access to your servers. Tmux gives it persistence — it keeps running even when you disconnect. ControlMaster gives it efficient, multiplexed connections. The proxy setup gives your servers internet access through Claude Code's machine. All of that was plumbing. This chapter is where you turn on the water.
Installation
Claude Code requires Node.js 18 or later. Check if you have it:
node --versionIf you see v18.x.x or higher, you're good. If not, install Node.js first. On Ubuntu:
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejsOn macOS with Homebrew:
brew install nodeNow install Claude Code:
npm install -g @anthropic-ai/claude-codeVerify the installation:
claude --versionYou should see a version number. That's it. Claude Code is a single npm package with no heavy dependencies. It runs on your local machine — no GPU needed, no Docker, no complicated setup.
Authentication
Claude Code needs access to Anthropic's API. There are two ways to authenticate.
Option 1: Interactive setup (recommended for first time)
Just run claude in any directory:
claudeIt will walk you through authentication on first launch. You'll be directed to log in or enter your API key. Follow the prompts.
Option 2: Environment variable
If you already have an API key, set it in your shell profile (~/.bashrc or ~/.zshrc):
export ANTHROPIC_API_KEY="YOUR_API_KEY"Then restart your shell or source the file. Claude Code will pick it up automatically.
Where do you get an API key? Go to console.anthropic.com, create an account, and generate a key. You'll need to add a payment method — Claude Code is not free, which brings us to the part nobody wants to talk about.
Cost Breakdown
Let's be honest. Claude Code costs real money. This is not a free tool with a generous tier. If you use it seriously for research, you will spend money every day. The question is how much, and whether it's worth it.
Claude Code uses Anthropic's API, and you pay per token — both input (what you send) and output (what it generates). Every time Claude Code reads a file, that's input tokens. Every time it writes code or gives you an answer, that's output tokens. Every time it runs a command and reads the result, that's more tokens. A typical research session involves a lot of file reading, code generation, and command execution. The tokens add up.
Here's what realistic daily usage looks like, depending on which model you choose:
Haiku — ~$1-3/day Fast. Cheap. Good for simple, routine tasks: checking file contents, running basic commands, monitoring status. Not great for complex reasoning or writing sophisticated code. Think of it as a capable but junior assistant.
Sonnet — ~$5-15/day The sweet spot for most research work. Solid at code generation, debugging, understanding complex codebases, and following multi-step instructions. This is probably your daily driver.
Opus — ~$15-40/day The most capable model. Best for complex reasoning tasks: designing experiments, reviewing paper drafts, debugging subtle issues, understanding intricate code. Use it when you need the best thinking, not for routine file operations.
These numbers assume moderate usage — a few hours of active interaction per day, with intermittent monitoring tasks. Heavy usage days (initial project setup, debugging marathons, paper writing) will be on the higher end. Light days (just monitoring training) will be lower.
Alternative: Claude Max subscription
Anthropic offers a monthly subscription plan called Claude Max that includes Claude Code usage. If you find yourself consistently spending more than the subscription price, it's worth switching. Check anthropic.com for current pricing.
The real question: is it worth it?
Consider this. A single idle GPU-hour on a cloud instance costs $1-4 depending on the GPU type. If Claude Code catches one silent failure per week that would have wasted 8 hours of GPU time, it has already paid for itself. And that's before counting the value of your own time — the weekends you get back, the sleep you don't lose, the mental overhead that disappears.
For a PhD student on a tight budget, start with Sonnet and set a daily spending alert. For a lab with multiple GPU servers, the cost of Claude Code is a rounding error on your compute bill.
Practical tip: Start with Sonnet for your daily work. Switch to Opus when you're tackling complex tasks — experiment design, tricky debugging, code architecture decisions. Use Haiku for routine monitoring and simple operations. You can change models mid-conversation.
Your First Conversation
Time to see it in action. Navigate to any project directory and start Claude Code:
cd ~/your-project
claudeYou'll see a prompt. Type something simple:
What files are in this directory?Watch what happens. Claude Code doesn't guess. It runs ls (or uses its file-reading tools), reads the output, and tells you what's there. It might also read a few files to understand the project structure.
Try something more interesting:
Read the training config and tell me what learning rate is set.It will find the config file, read it, and answer. No copy-pasting. No switching windows. No searching through your directory tree manually.
Now try giving it a task:
The batch size in config.yaml is 32. Change it to 16.It reads the file, makes the edit, and shows you the diff. You approve or reject the change.
This is the fundamental interaction pattern: you give instructions in plain English, Claude Code uses tools to carry them out, and you confirm the results. The tools include reading files, writing files, running shell commands, searching codebases, and more. Claude Code picks the right tools automatically — you don't need to tell it how to do things, just what to do.
One more thing to try — let Claude Code interact with your server:
SSH into my GPU server and check if there are any running tmux sessions.If your SSH config is set up from Chapter 2, Claude Code will connect and check. It uses the same ssh command you would. It reads the same config files. There's no special integration — it just runs commands in your terminal, exactly like you would, except faster and without typos.
Basic commands to know:
- Type naturally — Claude Code understands plain English instructions
/help— see available slash commands/model— switch between Haiku, Sonnet, and Opus mid-conversationCtrl+C— cancel the current operationCtrl+Dor/exit— end the session- Scroll up in tmux (
Ctrl+b [) to see earlier output — useful for reviewing what Claude Code did while you were away
The Key Insight: Claude Code in Tmux
Here is where everything from the previous chapters converges into a single, powerful pattern.
You learned SSH in Chapter 2. You learned tmux in Chapter 3. You set up ControlMaster in Chapter 4. Now combine them.
Start a tmux session on your local machine:
tmux new -s claudeInside that tmux session, start Claude Code:
claudeClaude Code is now running inside a persistent tmux session on your local machine. This is the foundation of the entire system. Here's why.
Detach. Press Ctrl+b then d. You're back at your regular terminal. Claude Code is still running in the background, inside tmux, exactly where you left it.
Walk away. Go to dinner. Go to bed. Go to a different city.
Come back from your phone. Open Termius. SSH into your local machine. Reattach:
tmux attach -t claudeThere it is. Claude Code, exactly where you left it. You can type instructions from your phone. Check training status. Launch a new experiment. Fix a crashed job. All from your phone, all through the tmux session, all because Claude Code is running in a persistent environment that doesn't depend on any particular connection staying alive.
This is the pattern that makes the entire guide work:
Phone (Termius) → SSH → Local Machine → tmux session → Claude Code → SSH → GPU ServerEvery link in this chain is persistent and reconnectable. Your phone disconnects? Doesn't matter — tmux holds the session. Your laptop goes to sleep? Doesn't matter — the local machine keeps running. The SSH connection to the server drops? ControlMaster reconnects automatically.
This is why we spent four chapters on infrastructure before installing Claude Code. Without SSH, Claude Code can't reach your servers. Without tmux, Claude Code dies when you disconnect. Without ControlMaster, every SSH command creates a new connection and steals the proxy tunnel. The infrastructure isn't optional — it's what transforms Claude Code from a local chatbot into a remote research assistant.
Running Claude Code Headless
Once Claude Code is running in tmux, it doesn't need you to be present. You can give it a set of instructions and let it work autonomously for hours.
This is called headless operation, and it's one of the most powerful patterns in this guide. Here's what it looks like in practice.
You attach to the tmux session, give Claude Code a complex task:
Sync the training code to server b2, activate the conda environment,
and launch training with the config in experiments/baseline.yaml.
Monitor the first 100 steps and make sure the loss is decreasing.
If it crashes, read the error log and fix the issue.Claude Code starts working. It syncs files with rsync. It SSHes into the server. It activates the environment. It runs the training command. It watches the output. You watch for a minute to make sure it's on the right track, then you detach from tmux and go live your life.
An hour later, you check in from your phone. Claude Code has launched the training, verified the loss is decreasing, and is monitoring the run. Or maybe it hit an error, fixed it, and restarted — you can scroll up in the tmux buffer to see exactly what happened.
The key mental shift is this: you stop thinking of Claude Code as a tool you use and start thinking of it as a colleague who works the night shift. You hand off work at the end of the day. You check the status in the morning. In between, things get done.
This is why tmux matters. Not just for keeping SSH sessions alive, but for keeping your AI assistant alive and working while you're away. Claude Code in tmux is an autonomous agent that can operate for hours without human input. It uses tools, makes decisions, handles errors, and keeps working toward the goal you gave it.
The later chapters will show you how to make this even more powerful with hooks (automatic actions triggered by events), watchdog monitoring (detecting failures without polling), and periodic health checks. But the foundation is simple: Claude Code, running in tmux, connected to your servers over SSH.
What You Should Not Expect
Claude Code is powerful, but it's not magic. A few things to keep in mind:
It makes mistakes. Sometimes it misreads a file, generates buggy code, or misunderstands your instructions. Always review significant changes before running them in production. This is an assistant, not an autopilot.
It doesn't have infinite context. Every conversation has a context window. In long sessions, earlier parts of the conversation get compressed or forgotten. For important information that must persist across sessions, you'll write it into files (CLAUDE.md) — that's the next chapter.
It costs money. We covered this above, but it bears repeating. Set spending alerts. Monitor your usage. Don't leave it running idle in a loop.
It's not a replacement for understanding. Claude Code can write training scripts, but you need to understand what the training script does. It can fix OOM errors, but you need to know what OOM means and whether the fix is appropriate. The AI handles the mechanical work. The scientific judgment is yours.
Long conversations degrade. After many back-and-forth exchanges, Claude Code's context gets full. Responses may slow down, or it may forget things from earlier in the conversation. When this happens, start a fresh session. Important context should live in files (particularly CLAUDE.md), not in conversation memory. We'll cover this in the next chapter.
Checkpoint
Start Claude Code inside a tmux session:
tmux new -s claude
claudeDetach with Ctrl+b d. SSH from your phone using Termius. Reattach:
tmux attach -t claudeTalk to Claude Code from your phone. Ask it to list the files in your home directory. If it responds, you now have an AI research assistant accessible from anywhere in the world.
That's it. That's the moment. Everything that follows builds on this.