Prerequisites
Before you begin, you need the following:- A macOS or Linux terminal with either Python 3.12 or later and
uv, or Node.js 22 or later. - A Forge API key or CoreWeave API access token.
- Credentials for a Pi-supported model provider. This guide uses Serverless Inference, which requires inference credits.
- A public Git repository URL. Private repositories require separate Git authentication inside the sandbox.
Choose sandbox credentials
These examples use serverless capacity. Both credentials support serverless sandboxes. See Choose and get credentials to obtain either credential. Agent and model-provider credentials are configured separately. After creating your local project in the next section, savesandbox_auth.py or sandbox-client.mts for your language beside the scripts you run from this guide. Use the same file for creation, reconnection, and cleanup. It stores your authentication choice, not the key itself.
- Forge API key
- CoreWeave API access token
Replace
[WANDB-API-KEY] with your Forge API key and export it in your local terminal:- Python
- TypeScript
sandbox_auth.py
Prepare your local environment
Create a local project and install the Sandboxes software development kit (SDK):- Python
- TypeScript
Create the sandbox
Save the script for your language ascreate_pi_sandbox.py or create_pi_sandbox.mts. It creates a CPU sandbox, installs Pi 0.87.1, and clones the repository. The Node.js image supplies the runtime and Git. If setup fails, the script stops the sandbox before reporting the error.
- Python
- TypeScript
create_pi_sandbox.py
[REPOSITORY-URL] with your repository URL, then run:
- Python
- TypeScript
0.87.1. The sandbox remains running after the script exits, until you stop it or its 4-hour lifetime expires. The lifetime includes startup time.
Attach your terminal
Save the script for your language asattach_sandbox.py or attach_sandbox.mts. It forwards your terminal input to a Bash shell in the sandbox and restores your local terminal settings when the shell exits.
- Python
- TypeScript
attach_sandbox.py
[SANDBOX-ID] with your sandbox ID:
- Python
- TypeScript
Choose a model provider
Choose one of the following options. Sandbox authentication and model authentication use separate credentials in this guide. Pi can access credentials provided to its process and can send workspace content to the selected model provider.Use Serverless Inference
We recommend Serverless Inference for this walkthrough. The example useszai-org/GLM-5.2. Choose another model from the model catalog if it better fits your task.
In the sandbox shell, enter a Forge API key for an account with inference credits at the hidden prompt:
~/.pi/agent/models.json file. The quoted heredoc preserves the environment-variable reference so the file doesn’t contain the key:
/workspace/project and zai-org/GLM-5.2 in the status area. Submit a task in Run Pi and check its work to verify model access.
The configuration limits each response to 8,192 tokens. If you change models, update the model ID and capabilities to match the provider. For more configuration options, see Pi model configuration.
Use another provider
To use Anthropic or OpenAI directly, choose the corresponding API key and start Pi in the same sandbox shell:- Anthropic
- OpenAI
/model inside Pi to choose a model available to your provider. The Pi provider documentation linked in the prerequisites covers other providers and account sign-in options.
Run Pi and check its work
If Pi displays a project trust prompt, follow it. To check that Pi can create and read a file in the cloned repository, submit this task in Pi:/quit to return to the sandbox shell. Pi prints a To resume this session: line with a pi --session command. Keep that command to reopen this conversation.
Verify the file independently:
Hello from CoreWeave.
For an unattended task with the Forge setup, run Pi in print mode from the same shell:
Reconnect to a conversation
To reconnect while the sandbox is running, run the attach script again. Re-enter any API keys supplied through the previous shell and return to the/workspace/project directory before starting Pi. To reopen your interactive conversation, replace [SESSION-ID] with the ID from the To resume this session: line Pi printed when you exited:
pi --continue resumes the most recent session for the project. If you ran the print-mode example after your interactive conversation, it resumes the print-mode session. Use --session to return to a specific conversation.
Retrieve the result and stop
Enterexit to leave the sandbox shell. In your local terminal, save the script for your language as collect_pi_result.py or collect_pi_result.mts:
- Python
- TypeScript
collect_pi_result.py
[SANDBOX-ID] with your sandbox ID and run the script:
- Python
- TypeScript
stop_pi_sandbox.py or stop_pi_sandbox.mts:
- Python
- TypeScript
stop_pi_sandbox.py
- Python
- TypeScript
Troubleshoot
Use these checks to resolve common issues:- If sandbox creation fails, check your sandbox credential and the sandbox authentication requirements linked in the prerequisites.
- If Pi can’t authenticate, check that the model credential is exported in the shell that starts Pi. With Forge, also check inference credits and the model ID.
- If Pi can’t reach its provider, check outbound access to the provider endpoint. Installation and cloning also require access to npm and your Git host.
- If your connection drops, attach again while the sandbox runs. If you need a process to survive terminal disconnections, use a terminal multiplexer such as
tmux.
Next steps
For more information, see the following resources:- Interactive shells covers terminal access through the SDK.
- File operations covers uploading and retrieving workspace files.
- Pi documentation covers sessions, model selection, and extensions.