architecture/06-cli.md
The Cog CLI is a Go binary that provides commands for the full model lifecycle: development, building, testing, and deployment. This document covers what each command does and how it connects to the systems described in previous docs.
Important: Model code always runs inside a container, never on the host machine. Commands like cog predict and cog serve build an image, start a container, and interact with it via the Prediction API. The CLI orchestrates this, but the model execution happens in the containerized Container Runtime.
| Command | Job To Be Done |
|---|---|
cog init | Bootstrap a new model project |
cog build | Create a container image |
cog predict | Run a prediction in a container |
cog exec | Run arbitrary commands in a container |
cog serve | Start HTTP server in a container |
cog push | Deploy to Replicate |
cog login | Authenticate with Replicate |
Job: Create a starter cog.yaml and predict.py for a new model.
cog init
Creates:
cog.yaml with sensible defaultspredict.py with a skeleton Predictor classCode: pkg/cli/init.go
Job: Run a prediction in a container.
cog predict -i prompt="A photo of a cat" -i steps=50
What happens:
-i flags against the SchemaInput types are inferred from the schema:
-i prompt="hello"-i steps=50-i [email protected] (uploaded to container)-i image=https://example.com/photo.jpgCode: pkg/cli/predict.go
Job: Run arbitrary commands in a container.
cog exec python -c "import torch; print(torch.cuda.is_available())"
cog exec bash
Builds the image (if needed), starts a container, and runs the specified command inside it. Useful for:
Code: pkg/cli/exec.go
Job: Start the HTTP server in a container for testing.
cog serve
# Server running at http://localhost:5000
Builds the image (if needed) and starts a container running the Container Runtime. The container's port 5000 is exposed to the host. You can then:
POST /predictions/openapi.json/health-checkCode: pkg/cli/serve.go
Job: Build a container image from Model Source.
cog build -t my-model
What happens (see Build System for details):
cog.yamlKey flags:
-t, --tag: Image tag--no-cache: Disable Docker cache--separate-weights: Exclude weights from image (for separate upload)Code: pkg/cli/build.go, pkg/image/build.go
Job: Build and push to Replicate.
cog push r8.im/username/model-name
What happens:
cog build)The image tag must be a Replicate model reference (r8.im/owner/name).
Code: pkg/cli/push.go, pkg/web/
Job: Authenticate with Replicate.
cog login
# or
cog login --token-stdin < token.txt
Stores credentials for cog push.
Code: pkg/cli/login.go
These commands exist but are hidden from cog --help:
cog debug -- Generates the Dockerfile from cog.yaml without building (useful for debugging build issues)cog weights -- Parent command for weights build, weights push, weights inspectThere's also a separate base-image binary (cmd/base-image/) with subcommands for managing Cog base images (dockerfile, build, generate-matrix). This isn't a cog subcommand.
Commands like predict and serve follow the same pattern: build an image, start a container, communicate via HTTP. The CLI never runs model code directly.
sequenceDiagram
participant CLI as cog CLI (host)
participant Docker
participant Container as Container (runtime)
CLI->>CLI: Parse -i flags, load cog.yaml
CLI->>Docker: Build image (if needed)
Docker-->>CLI: Image ready
CLI->>Docker: Start container
Docker->>Container: python -m cog.server.http
Container->>Container: Run setup()
loop Until READY
CLI->>Container: GET /health-check
Container-->>CLI: Status (STARTING/READY)
end
CLI->>Container: POST /predictions
Container->>Container: Run predict()
Container-->>CLI: Response JSON
CLI->>Docker: Stop container
For what happens inside the container (setup, predict, IPC), see Container Runtime.
The CLI is built with Cobra (Go CLI framework).
cmd/cog/
└── cog.go # Entry point
pkg/cli/
├── root.go # Root command, subcommand registration
├── build.go # cog build
├── predict.go # cog predict
├── exec.go # cog exec
├── serve.go # cog serve
├── push.go # cog push
├── login.go # cog login
└── init.go # cog init
Commands delegate to packages under pkg/:
Core:
pkg/cli/ -- Cobra command definitionspkg/config/ -- cog.yaml parsing and validation, compatibility matricespkg/image/ -- Build orchestration (ties together config, Dockerfile generation, schema gen)pkg/dockerfile/ -- Dockerfile generation and base image selectionpkg/docker/ -- Docker client operationspkg/predict/ -- Local prediction execution (talks to container's HTTP API)pkg/schema/ -- Static schema generator (tree-sitter, experimental)pkg/wheels/ -- SDK and coglet wheel resolutionInfrastructure:
pkg/web/ -- Replicate API client (for cog push)pkg/http/ -- Authenticated HTTP transportpkg/registry/ -- OCI/Docker registry clientpkg/model/ -- OCI artifact domain modelpkg/weights/ -- Weight file discovery and checksumspkg/errors/ -- CodedError for user-facing errors with error codesUtilities:
pkg/dockercontext/ -- Docker build context directory managementpkg/dockerignore/ -- .dockerignore parsingpkg/requirements/ -- requirements.txt parsingpkg/env/ -- R8_* environment variable configpkg/update/ -- CLI version update checkerpkg/global/ -- Build-time metadata, process-wide configpkg/provider/ -- Abstracts registry-specific behavior for push workflows