Workspaces with an agent
Let an AI agent create, start, stop and delete workspaces, and manage their packages.
Last updated
On this page
Description and operation
General
An agent creates, starts, stops and deletes a workspace with the workspace tools of its session. A workspace that the agent creates is managed: the session deletes it when the session ends. For what a workspace is, see Workspaces.
Each tool except workspace_create and workspace_list takes workspace, the id of the workspace. The id starts with ws-; the name does not work. workspace_list shows the workspaces of the session with their ids. The agent changes files and processes with the tool server tools. It takes and restores snapshots as in Volumes with an agent.
Each lifecycle tool returns at once and the work continues. Poll workspace_get until state is ready, stopped, error or deleted.
Tasks
Create a workspace
Reason for the task: Get a new workspace to work in.
Job set-up
- Access: you. A team workspace needs team membership.
- Agent tool:
workspace_create - References:
POST /v1/sessions/{id}/workspaces.
Procedure
Create the workspace in the session.
nameandquota_gbare required; addimageorpackagesas needed.{"tool": "workspace_create", "arguments": {"name": "backend", "quota_gb": 20}}
Close-out
- Show the status as in task 002. Wait until
stateisready.
The same task with the SDK or CLI: Create a workspace in a session.
Show the status of a workspace
Reason for the task: Check whether a workspace runs, and why not.
Job set-up
- Access: you.
- Agent tool:
workspace_get - References:
GET /v1/workspaces/{id}.
Procedure
Show the workspace. The agent tool takes the
id.{"tool": "workspace_get", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Make sure that
stateis what you expect. If not, see Troubleshooting.
The same task with the SDK or CLI: Workspaces task 002.
Start a workspace
Reason for the task: Run a stopped workspace.
Job set-up
- Access: you.
- Agent tool:
workspace_start - References:
POST /v1/workspaces/{id}/start.
Procedure
Start the workspace.
{"tool": "workspace_start", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Show the status as in task 002. Wait until
stateisready.
The same task with the SDK or CLI: Workspaces task 003.
Stop a workspace
Reason for the task: Release the compute of a workspace and keep its home.
Job set-up
- Access: you.
- Agent tool:
workspace_stop - References:
POST /v1/workspaces/{id}/stop.
All processes end and all connections close.
Procedure
Stop the workspace.
{"tool": "workspace_stop", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Show the status as in task 002. Make sure that
stateisstopped.
The same task with the SDK or CLI: Workspaces task 004.
Delete a workspace
Reason for the task: Remove a workspace that you no longer need.
Job set-up
- Access: you.
- Agent tool:
workspace_delete - References:
DELETE /v1/sessions/{id}/workspaces/{workspace}, Take a snapshot of a workspace.
The home is deleted. Work that is not in a snapshot is lost.
Procedure
Delete the workspace.
{"tool": "workspace_delete", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Make sure that
stateisdeleted.
The same task with the SDK or CLI: Delete a workspace in a session.
Add a package
Reason for the task: Install a tool in a workspace.
Job set-up
- Access: you.
- Agent tool:
packages_add - References: Find a package,
PATCH /v1/workspaces/{id}, The kl command.
Procedure
Add the package, with
@versionto pin it. An entry with the same name replaces the old entry.{"tool": "packages_add", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b", "packages": ["nodejs@22"]}}
Close-out
- Show the packages as in task 007. Make sure that the entry and its lock are listed.
The same task with the SDK or CLI: Packages task 001.
Show the packages
Reason for the task: Check which tools and versions a workspace has.
Job set-up
- Access: you.
- Agent tool:
packages_list - References:
GET /v1/workspaces/{id}.
Procedure
Show the packages and locks. Inside the workspace,
kl pkg listdoes the same.{"tool": "packages_list", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Make sure that every package that you expect is listed.
The same task with the SDK or CLI: Packages task 002.
Remove a package
Reason for the task: Remove a tool that you no longer need.
Job set-up
- Access: you.
- Agent tool:
packages_remove - References:
PATCH /v1/workspaces/{id}.
Procedure
Remove the package by name, without a version. Inside the workspace,
kl pkg rm jqdoes the same.{"tool": "packages_remove", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b", "packages": ["jq"]}}
Close-out
- Show the packages as in task 007. Make sure that the entry is gone.
The same task with the SDK or CLI: Packages task 003.
Update the locked versions
Reason for the task: Move each pinned package to the newest cached release that matches its version.
Job set-up
- Access: you.
- Agent tool:
packages_update - References:
POST /v1/workspaces/{id}/packages/update.
Procedure
Run the update. Inside the workspace,
kl pkg updatedoes the same.{"tool": "packages_update", "arguments": {"workspace": "ws-1a2b3c4d5e6f7a8b"}}
Close-out
- Show the packages as in task 007. Make sure that the locks show the expected versions.
The same task with the SDK or CLI: Packages task 004.
Troubleshooting
| Symptom | Possible cause | Corrective action |
|---|---|---|
409 with workspaces: 5 of 5 in use; request more under Quota | The quota of the owner is used up. The first word names the dimension. | Delete unused workspaces or snapshots, or request more quota. |
409 with a workspace named "backend" already exists here | The owner already has a workspace with this name. | Use another name, or delete the old workspace. |
409 with workspace is interrupted: its node is down | The machine that ran the workspace failed. | Wait for it to resume, or clone it. |
state stays creating and packages_status is Building | Packages are being installed. | Wait, then show the status. |
422 with is not a version anyone published; nearest: … | The version does not exist. | Use one of the versions in the message. |
packages_status is NotCached | A locked release has no prebuilt binary. | Add the package with a cached version. |
403 with your access to acme is paused | A team admin paused your access. | Ask a team admin to unpause it. |