Environments and intercepts with an agent
Let an AI agent run environments and services, connect a session to an environment and intercept a service.
Last updated
On this page
- Description and operation
- General
- Tasks
- Create an environment
- Show the status of an environment
- Start an environment
- Stop an environment
- Delete an environment
- Add a service
- Change a service
- Remove a service
- Read the logs of a service
- Connect an environment to a session
- Change or disconnect the environment
- Intercept a service
- Show the intercepts
- Remove an intercept
- Troubleshooting
Description and operation
General
An agent runs an environment with the environment tools. For what an environment is, see Environments. Each tool except env_create and env_list takes env, the id of the environment. The id starts with env-; the name does not work. env_list shows the environments of a team with their ids.
Each change tool returns at once. env_wait waits until every service is ready, a service is failing, or state is error or deleted. It takes timeout in seconds (default 300, maximum 900) and returns timedOut: true if the wait ends first. A service with failing: true does not come up by waiting: read its message, fix the cause and change the service again.
A session connects to one environment of its own team, and a workspace of the session reaches its services by name. The agent connects it with session_connect and takes the traffic of a service with intercept. See Sessions and Intercepts.
service_logs reads the recent output of one service. It takes service, tail (default 200, maximum 2000), since (seconds, maximum 86400) and previous (the run before a crash). It starts an idle-stopped environment first. If an intercept holds the service, the answer names the workspace that serves it.
Tasks
Create an environment
Reason for the task: Run the services of an application for workspaces to develop against.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
env_create - References:
POST /v1/environments, Services.
Procedure
Create the environment with its services. Give
snapshot_idinstead to grow it from a snapshot.{"tool": "env_create", "arguments": {"name": "acme-dev", "services": [{"name": "db", "image": "postgres:16", "ports": [5432], "env": {"POSTGRES_PASSWORD": "dev"}, "mounts": [{"folder": "pgdata", "path": "/var/lib/postgresql/data"}]}], "quota_gb": 20}}
Close-out
- Wait with
env_wait. Make sure thatstateisrunningand every service is ready.
The same task with the SDK or CLI: Environments task 001.
Show the status of an environment
Reason for the task: Check whether an environment and its services run, and why not.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
env_get - References:
GET /v1/environments/{id},GET /v1/environments.
Procedure
Show the environment. Each entry of
serviceshasready;env_listshows all environments of a team.{"tool": "env_get", "arguments": {"env": "env-1f2e3d4c5b6a7988"}}
Close-out
- Make sure that
stateand thereadyvalue of each service are what you expect. If not, see Troubleshooting.
The same task with the SDK or CLI: Environments task 002.
Start an environment
Reason for the task: Run a stopped environment.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
env_start - References:
POST /v1/environments/{id}/start.
Procedure
Start the environment.
{"tool": "env_start", "arguments": {"env": "env-1f2e3d4c5b6a7988"}}
Close-out
- Wait with
env_wait. Make sure thatstateisrunningand every service is ready.
The same task with the SDK or CLI: Environments task 003.
Stop an environment
Reason for the task: Release the compute of an environment and keep its data.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
env_stop - References:
POST /v1/environments/{id}/stop.
All services stop. Connected workspaces lose access to them until the environment starts again.
Procedure
Stop the environment.
{"tool": "env_stop", "arguments": {"env": "env-1f2e3d4c5b6a7988"}}
Close-out
- Make sure that
stateisstopped. Awarningin the response means that the machine of the environment is down.
The same task with the SDK or CLI: Environments task 004.
Delete an environment
Reason for the task: Remove an environment that you no longer need.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
env_delete - References:
DELETE /v1/environments/{id}, Volumes.
The services and the disk are deleted. Data that is not in a snapshot is lost.
Procedure
Delete the environment.
{"tool": "env_delete", "arguments": {"env": "env-1f2e3d4c5b6a7988"}}
Close-out
- Make sure that the environment is no longer listed. A
warningin the response names a session that was not disconnected.
The same task with the SDK or CLI: Environments task 006.
Add a service
Reason for the task: Run another program in an environment.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
service_add - References:
PATCH /v1/environments/{id}.
Procedure
Add the service.
{"tool": "service_add", "arguments": {"env": "env-1f2e3d4c5b6a7988", "service": {"name": "cache", "image": "redis:7", "ports": [6379]}}}
Close-out
- Wait with
env_wait. Make sure thatcacheis ready.
The same task with the SDK or CLI: Services task 001.
Change a service
Reason for the task: Change the image, variables, ports or mounts of a service.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
service_update - References:
PATCH /v1/environments/{id}.
The service restarts and its connections end. Data in its mounted folders is kept.
Procedure
Send the changed service, here
dbonpostgres:17. The agent tool replaces the whole service, so give every field.{"tool": "service_update", "arguments": {"env": "env-1f2e3d4c5b6a7988", "service": {"name": "db", "image": "postgres:17", "ports": [5432], "env": {"POSTGRES_PASSWORD": "dev"}, "mounts": [{"folder": "pgdata", "path": "/var/lib/postgresql/data"}]}}}
Close-out
- Wait with
env_wait. Make sure that the service shows the new value and is ready.
The same task with the SDK or CLI: Services task 002.
Remove a service
Reason for the task: Stop a program and take it out of an environment.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
service_remove - References:
PATCH /v1/environments/{id}.
Procedure
Remove the service by name. Its folders stay on the disk.
{"tool": "service_remove", "arguments": {"env": "env-1f2e3d4c5b6a7988", "name": "cache"}}
Close-out
- Make sure that the service is no longer in
services.
The same task with the SDK or CLI: Services task 003.
Read the logs of a service
Reason for the task: See what a service wrote, for example before it failed.
Job set-up
- Access: you. A team environment needs team membership.
- Agent tool:
service_logs - References: Services.
Procedure
Read the last 200 lines. Add
"previous": trueto read the run before a crash.{"tool": "service_logs", "arguments": {"env": "env-1f2e3d4c5b6a7988", "service": "db", "tail": 200}}
Close-out
- Make sure that the lines come from the service that you named.
Connect an environment to a session
Reason for the task: Give the workspaces of the session the services of an environment.
Job set-up
- Access: you.
- Agent tool:
session_connect - Conditions: the environment belongs to the team of the session.
- References:
PUT /v1/sessions/{id}/environment,kloudlite sessions connect-env.
Procedure
Connect the environment by name or
id. The tool acts on the session that the agent serves.{"tool": "session_connect", "arguments": {"environment": "shop"}}
Close-out
- Show the session with
session_current. Make sure thatenvironmentshows the environment.
The same task with the SDK or CLI: Sessions task 004.
Change or disconnect the environment
Reason for the task: Move a session to another environment, or detach it.
Job set-up
- Access: you.
- Agent tool:
session_disconnect - References:
PUT /v1/sessions/{id}/environment,kloudlite sessions disconnect-env.
The session releases its intercepts in the old environment.
Procedure
Set the session to no environment.
{"tool": "session_disconnect", "arguments": {}}Connect the new environment as in task 010 to change it.
Close-out
- Show the session with
session_current. Make sure thatenvironmentshows the newid, or no value after a disconnect.
The same task with the SDK or CLI: Sessions task 005.
Intercept a service
Reason for the task: Run a service from your workspace against the rest of the environment.
Job set-up
- Access: you.
- Agent tool:
intercept - Conditions: the session is connected to the environment. The workspace is in the session and runs. The service declares a port.
- References:
POST /v1/environments/{id}/intercepts.
Procedure
Create the intercept. Map ports with
ports: this example sends 8080 to 3000.{"tool": "intercept", "arguments": {"env": "env-1a2b3c4d5e6f7a8b", "service": "api", "workspace": "ws-1a2b3c4d5e6f7a8b", "ports": [{"service": 8080, "workspace": 3000}]}}
Close-out
- Get the environment with
env_get. Make sure that the service inservice_statusshowsinterceptedByand thatproxyisready.
The same task with the SDK or CLI: Intercepts task 001.
Show the intercepts
Reason for the task: Check which services your session routes to a workspace.
Job set-up
- Access: you.
- Agent tool:
env_get - References:
GET /v1/environments/{id}.
Procedure
Show the environment. The answer lists every intercept of the environment, with its session.
{"tool": "env_get", "arguments": {"env": "env-1a2b3c4d5e6f7a8b"}}
Close-out
- Make sure that every service that you intercepted is listed.
The same task with the SDK or CLI: Intercepts task 002.
Remove an intercept
Reason for the task: Give a service its traffic back.
Job set-up
- Access: you.
- Agent tool:
release - References:
DELETE /v1/environments/{id}/intercepts/{service}.
Procedure
Remove the intercept.
{"tool": "release", "arguments": {"env": "env-1a2b3c4d5e6f7a8b", "service": "api"}}
Close-out
- Show the intercepts as in task 013. Make sure that the service is gone.
The same task with the SDK or CLI: Intercepts task 003.
Troubleshooting
| Symptom | Possible cause | Corrective action |
|---|---|---|
409 with environments: 5 of 5 in use; request more under Quota | The quota of the owner is used up. The first word names the dimension, for example diskGb or cpu. | Delete unused environments, or request more quota. |
409 with environment is interrupted: its node is down | The machine that holds the environment failed. | Wait for it to resume. |
409 with this session has no environment | The session has no environment. | Connect an environment. |
409 with that workspace is not a member of a session on this environment; add it to a session connected to it | The workspace is not in a session that is connected to this environment. | Use a workspace of the connected session, or connect the environment. |
409 with that workspace is stopped; start it before it can answer for a service | The workspace is not running. | Start the workspace. |
409 with … already intercepts this service; it must be released first | Another workspace holds the intercept. The message names the holder. | Ask the holder to release it. |
422 with service api declares no ports; nothing to intercept | The service has no port to route. | Change the service and add a port. |
422 with would land on workspace port 7788 | The mapping targets the tool server. | Map to another workspace port. |
proxy is failed | The switch did not complete. The service keeps its traffic. | Remove the intercept and create it again. |