Install Cody CLI
Learn how to install the cody
command-line tool and using the cody chat
subcommand.
Cody CLI support is in the experimental stage.
Cody CLI is the same technology that powers the Cody IDE plugins but available from the command-line. Use Cody CLI for ad-hoc exploration in your terminal or as part of scripts to automate your workflows.
Cody CLI is available to Free, Pro, and Enterprise customers.
Prerequisites
- You have Node.js
v20
or newer installed - You have
npm
,yarn
,pnpm
, or an equivalent package manager installed
Install CLI from npm
Run the following command to install the Cody CLI:
SHELLnpm install -g @sourcegraph/cody
Confirm that the installation was successful by running the following command:
SHELLcody help
Authenticate with cody auth login
To start using the Cody CLI, you need to authenticate it with your Sourcegraph account.
When using cody auth login
, the access token is stored in the secure storage of your operating system or equivalent security tool.
If you prefer not to let Cody store your access token, authenticate with environment variables instead.
First, make sure you have installed the necessary tools to use cody auth login
.
The pre-installed security
tool is used. No additional installation is required.
Run the following command to authenticate the Cody CLI:
SHELLcody auth login --web
This will open a browser window where you can authenticate with your Sourcegraph account. Close the browser tab after authentication is complete.
Confirm that the authentication was successful by running the following command:
SHELLcody auth whoami # ✔ Authenticated as USERNAME on ENDPOINT
Authenticate with only environment variables
Skip this step if you have already authenticated with the cody auth login
command.
If you prefer not to let Cody CLI store your access token, you can also pass the endpoint URL and access token through the environment variables SRC_ENDPOINT
and SRC_ACCESS_TOKEN
.
SHELLexport SRC_ENDPOINT=ENDPOINT export SRC_ACCESS_TOKEN=ACCESS_TOKEN
It's recommended to store these access tokens in a secure location. For example, you can store them with a password manager like 1Password or Bitwarden.
It is not recommended to export these variables in your shell startup script because it will expose your access token to all commands you run from the terminal. Instead, consider sourcing these environment variables on-demand when you need to authenticate with the Cody CLI.
Sign out with cody auth logout
To sign out of the Cody CLI, run the following command:
SHELLcody auth logout
Running this command wil remove the access token from the secure storage of your operating system.
Run cody auth whoami
to confirm that the Cody CLI is no longer authenticated.
Chat with basic message
Once you've authenticated the Cody CLI, you can start using it to chat with Cody.
To start a new chat, run the following command:
SHELLcody chat -m 'Explain React hooks'
The following commands are equivalent to the above:
SHELL# use --message instead of -m cody chat --message 'Explain React hooks' # space separated arguments cody chat Explain React hooks
Chat with --context-file to add context from local files
Use --context-file
to provide context from local files
SHELLcody chat --context-file src/controller.ts -m 'Are there code smells in this file?'
Chat with --context-repo to add context from remote repositories
Use --context-repo
to provide context from remote repositories.
SHELLcody chat --context-repo github.com/sourcegraph/cody -m 'What is the agent?'
Chat with --stdin to read message from standard input
Use --stdin
to provide context from standard input
SHELLecho 'Explain React hooks' | cody chat --stdin
Combine --stdin
with --message
to send a concatenated message. The
--message
string appears at the top of the prompt and the --stdin
text
appears at the bottom.
SHELLgit diff | cody chat --stdin -m 'Write a commit message for this diff'
Use the -
trailing argument as an alternative to --stdin
to read the diff from standard input.
SHELLgit diff | cody chat -m 'Write a commit message for this diff' -