| name | clarifai-deployment-lifecycle |
| description | Manage the end-to-end lifecycle of Clarifai deployments. Use when deploying, updating, monitoring, or removing model deployments. Covers the CLI-first deploy/status/logs/undeploy workflow, version tracking with metadata, the Delete-then-Post cycle, and state transition monitoring. |
Clarifai Deployment Lifecycle
Managing deployments involves the full lifecycle: deploy, monitor, update, and teardown. The CLI provides the primary interface for all these operations.
CLI-First Deployment (Recommended)
The clarifai model deploy command handles the entire deployment lifecycle with zero interactive prompts:
clarifai model deploy ./my-model
clarifai model deploy ./my-model --instance gpu-nvidia-a10g
clarifai model deploy --model-url "https://clarifai.com/user/app/models/my-model"
clarifai model status --deployment <deployment-id>
clarifai model logs --deployment <deployment-id>
clarifai model logs --deployment <deployment-id> --log-type events
clarifai model undeploy --deployment <deployment-id>
When to use --deployment vs --model-url:
- Use
--deployment <id> when you have the deployment ID (printed by clarifai model deploy)
- Use
--model-url <url> when you don't know the deployment ID and want to discover deployments for a model
What clarifai model deploy Does
- Uploads and builds the model (if local path provided)
- Discovers or creates compute cluster and nodepool based on
--instance or config.yaml
- Creates deployment with autoscaling (default: 1-5 replicas)
- Monitors until pods are running and healthy
- Zero prompts - fully non-interactive, suitable for CI/CD
Browse Available GPUs
clarifai list-instances
clarifai list-instances --cloud aws --gpu A10G
clarifai list-instances --min-gpu-mem 80Gi
Advanced: Programmatic Deployment
Version Patching (The Refresh Cycle)
When model code is updated in 1/model.py, a new version must be uploaded, and the active deployment must be pointed to that new version. Since deployments are currently static, the standard pattern is Delete-then-Post.
Automated Re-deployment Pattern
from clarifai.client.user import User
from clarifai_grpc.grpc.api import service_pb2, resources_pb2
from clarifai_grpc.grpc.api.status import status_code_pb2
import time
def refresh_deployment(app, deployment_id, model_id, version_id, nodepool_id):
list_req = service_pb2.ListDeploymentsRequest(user_app_id=app.user_app_id)
list_resp = app.STUB.ListDeployments(list_req, metadata=app.metadata)
existing = next((d for d in list_resp.deployments if d.id == deployment_id), None)
if existing:
print(f"Deleting existing deployment {deployment_id}...")
del_req = service_pb2.DeleteDeploymentsRequest(
user_app_id=app.user_app_id,
ids=[deployment_id]
)
app.STUB.DeleteDeployments(del_req, metadata=app.metadata)
time.sleep(2)
deployment = resources_pb2.Deployment(
id=deployment_id,
worker=resources_pb2.Worker(
model=resources_pb2.Model(
id=model_id,
model_version=resources_pb2.ModelVersion(id=version_id)
)
),
nodepools=[resources_pb2.Nodepool(id=nodepool_id)]
)
request = service_pb2.PostDeploymentsRequest(
user_app_id=app.user_app_id,
deployments=[deployment]
)
return app.STUB.PostDeployments(request, metadata=app.metadata)
Version Tracking with Metadata
Always use the metadata field in ModelVersion to track the source of the deployment. This is visible in logs and via the API, making it clear which Git commit or branch is currently running in production.
Setting Metadata during Upload
version = resources_pb2.ModelVersion(
metadata=resources_pb2.MetaData(
fields={
"git_commit": resources_pb2.Value(string_value="664cf8a..."),
"git_branch": resources_pb2.Value(string_value="main"),
"environment": resources_pb2.Value(string_value="production")
}
)
)
Deployment States and Status Monitoring
Understanding the progression of a deployment is critical for automated workflows. You should always verify that a deployment is READY before attempting inference.
Monitoring Readiness via SDK
The create_deployment() method in the Nodepool class handles waiting and log streaming by default via the wait=True argument.
deployment = nodepool.create_deployment(config_filepath="config.yaml", wait=True)
If you prefer to handle waiting manually (e.g., in a non-interactive script), set wait=False and use runner_metrics():
deployment = nodepool.deployment("my-deployment")
while True:
metrics = deployment.runner_metrics()
if metrics["pods_running"] > 0:
break
print(f"Waiting for deployment... Total pods: {metrics['pods_total']}, Running: {metrics['pods_running']}")
time.sleep(10)
Manual Status Check (Runner Metrics)
If using the gRPC API directly, you must list the Runners for the specific nodepool and check their runner_metrics field. A deployment is considered ready when pods_running >= 1.
| State | Condition | Description |
|---|
| READY | pods_running >= 1 | At least one pod is healthy and accepting traffic. |
| INITIALIZING | pods_total >= 1 and pods_running == 0 | Resources are assigned but pods are still starting up or pulling images. |
| QUEUED | pods_total == 0 | No resources have been assigned by the scheduler yet. |
Best Practices
CLI Deployment Management
You can manage deployments directly via the CLI:
clarifai deployment list
clarifai deployment list --nodepool_id <nodepool_id>
clarifai deployment list --compute_cluster_id <compute_cluster_id>
clarifai deployment delete <deployment_id>
Creating a Deployment (clarifai deployment create)
To create a deployment via CLI, you must provide a config.yaml file that defines the deployment configuration.
clarifai deployment create --config deployment_config.yaml
Wait and Log Behavior:
When min_replicas > 0, the CLI will automatically:
- Stream
runner and runner.events logs to your terminal to provide visibility into the initialization or potential errors.
- Wait and poll the deployment status until the number of running replicas reaches
min_replicas AND at least one log entry has been received from the infrastructure.
- Return only after a successful deployment is confirmed and initialized.
If min_replicas is 0, the CLI displays a warning and returns immediately, as actual infrastructure replicas will only be provisioned upon the first prediction request (Cold Start pattern).
Deployment Config Structure (deployment_config.yaml)
The configuration file follows a structured YAML format matching the PostDeployments request:
deployment:
id: "my-deployment-id"
user_id: "your-user-id"
worker:
model:
id: "model-id"
model_version:
id: "version-id"
user_id: "model-owner-id"
app_id: "app-id"
nodepools:
- id: "nodepool-id"
compute_cluster:
id: "cluster-id"
user_id: "cluster-owner-id"
autoscale_config:
min_replicas: 1
max_replicas: 5
scale_to_zero_delay_seconds: 3600
deploy_latest_version: true
Automated Deployment Strategy
Preferred approach: Use clarifai model deploy which handles everything automatically:
clarifai model deploy ./my-model
clarifai model deploy ./my-model --instance gpu-nvidia-a10g
The deploy command automatically:
- Searches for existing compute clusters and nodepools in the user's account
- Creates deterministic cluster/nodepool IDs based on cloud/region/instance
- Reuses existing infrastructure if it matches, creates new if not
Manual fallback (when clarifai model deploy isn't suitable):
- Browse instances:
clarifai list-instances --cloud aws --gpu A10G
- Resource Discovery: List available
ComputeClusters and Nodepools via SDK
- Heuristic Matching: Compare model's
inference_compute_info against available instance types
- Agent-Led Provisioning: If no match, ask user before creating infrastructure