Where to run#
| Setting | Default | Note |
|---|
| Local NVIDIA GPU | $0 | Install CUDA + cuDNN. Regular use, large datasets |
|---|
| Google Colab (free tier) | $0 | Runtime > Change runtime type > T4 GPU |
|---|
| Cloud GPU (Lambda, RunPod, Vast.ai) | $0.20-2.00/hr | SSH + install. Serious training |
|---|
| CPU | phases 1-3 | Most lessons run fine on CPU |
|---|
| fp16 rule of thumb | 2 bytes per parameter | Keep a margin for activations |
|---|
Check the GPU#
torch.cuda.is_available()False means no GPU
torch.version.cudaPyTorch CUDA version
torch.cuda.get_device_name(0)GPU name
props = torch.cuda.get_device_properties(0)
vram_gb = props.total_memory / 1e9VRAM in GB
params_fp16 = vram_gb * 1e9 / 2max parameters in fp16
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.cuda.synchronize()before and after a GPU timing
Store and use an API key#
First call with the SDK#
client = anthropic.Anthropic()
MODEL = os.environ.get("LLM_MODEL", "claude-sonnet-5")model id
response = client.messages.create(
model=MODEL,
max_tokens=256,
messages=[{"role": "user", "content": "<prompt>"}]
)
print(response.content[0].text)reply text
response.usage.input_tokens, response.usage.output_tokensbilled tokens
Raw HTTP call#
| Setting | Default | Note |
|---|
| Endpoint | https://api.anthropic.com/v1/messages | method="POST" |
|---|
x-api-key | ANTHROPIC_API_KEY | Key header |
|---|
anthropic-version | 2023-06-01 | Version header |
|---|
LLM_MODEL | claude-sonnet-5 | Un-dated Sonnet alias |
|---|
max_tokens | 256 | Lesson value |
|---|
MOCK | "1" | TypeScript port then skips the network |
|---|
Build and run the dev image#
docker build -t ai-dev -f phases/00-setup-and-tooling/07-docker-for-ai/code/Dockerfile .
docker run --rm -it --gpus all \
-v $(pwd):/workspace \your code
-v ~/models:/models \models survive rebuilds
-p 8888:8888 \Jupyter port
ai-dev jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
Docker tasks#
| Task | Code |
|---|
| Start the stack | docker compose up -d |
|---|
| Stop and delete the volume | docker compose down -v |
|---|
| Shell in a service | docker compose exec ai-dev bash |
|---|
| Image sizes | docker images |
|---|
| Reclaim disk | docker system prune -a |
|---|
| GPU in a container | docker exec -it <container_id> nvidia-smi |
|---|
| Follow logs | docker logs -f <container_id> |
|---|