Using AI Coding Agents

Coder workspaces provide a relatively safe way to experiment with AI coding agents, like Claude Code, Gemini CLI, or Open AI’s Codex. These tools, which generate and also execute code, can be useful but they are also inherently dangerous. “Prompt injection” attacks can trick AI tools into running nefarious code on your system. Because Coder workspaces are isolated virtual machines, the potential damage a rogue AI agent running in a workspace can cause is reduced (but not eliminated).

Note

To use an AI coding agent, you typically need an account from a model provider like Google, OpenAI, or Anthropic. UCSB faculty and staff can use their UCSB email to authenticate Google’s Gemini CLI. Unfortunately, UCSB students cannot access Gemini using their UCSB accounts at this time.

Getting Started

Most AI coding agents run in the terminal and are distributed as NodeJS applications. We’ll use the terminal to install nodejs and the agent itself. You can use the default web-based terminal that is included with every workspace to run the shell commands below. (If you prefer to use VS Code’s built-in terminal, then you will need to enable VS Code when you create the workspace.)

# use pixi to install nodejs and npm (package manager)
pixi global install nodejs

Now we need to update the shell’s $PATH to include the “global” directory where the NodeJS package manager (npm) will install the agent:

# update our current $PATH variable
echo "export PATH=\$PATH:\$(npm prefix -g)/bin" >> ~/.bashrc

# re-run ~/.bashrc to activate our new $PATH settings
source ~/.bashrc

Create a project directory

You shouldn’t run the agent from the home directory (~). To start a new project, make a new directory and set it is as the working directory.

# start a new project
mkdir my-project

# or `git clone` an existing one
# git clone git@github.com:my-account/my-project.git
 
# set the project as the working directory
cd my-project

We’re ready to install and run the agent!

Installing the agent

To install the Gemini CLI:

# install gemini as a global command
npm install -g @google/gemini-cli

# run gemini (inside the project directory)
gemini

Example Prompt

Here is an example prompt to generate a map based on COVID-19 fatality data using R.

Calculate the Local Moran's I statistic for covid-19 fatality rates 
in US counties (contiguous 48 states only), then make a map of counties 
in the continental US showing outliers. A csv with covid fatality rates 
is available at this URL: 

https://dreamlab-public.s3.us-west-2.amazonaws.com/ocfl/model-tasks/v1/content/data/USCounties_cases.csv

Make an R script called 'map.R' to generate the map. The map should be 
saved to 'map.png'. Use "tidy" R conventions. You are running in a headless 
environment, without a graphical display.