How does an AI agent rent a GPU?
An AI agent estimates GPU memory and searches offers. If a matching GPU is bookable, it can request a quote and book with a capped key.
What to do
| Step | MCP tool | REST route |
|---|---|---|
| Estimate memory and search | fit_model then search_offers | GET /v1/fit then GET /v1/offers |
| Review and book | get_quote then create_instance | POST /v1/quotes then POST /v1/instances |
| Check status | get_resource | GET /v1/resources/{id} |
| Stop and read the bill | control_resource then get_receipt | POST /v1/resources/{id}/actions then GET /v1/receipts/{id} |
Try it with curl
Set X9_API_URL to the base URL of your X9 API, without a trailing slash. Use the production API at https://prod-api.x9si.com. Run in Bash with curl and jq.
Offer search needs no X9 key. Quote, rent, stop and receipt send Authorization: Bearer with your capped X9_API_KEY. JSON writes send Content-Type: application/json and an Idempotency-Key. Set and save a unique X9_RUN_ID before you start, such as one from uuidgen.
The example searches for a consumer GPU with at least 16 GB of memory. It does not estimate the memory your model needs. It searches bookable offers from X9 hosts only and follows next_cursor until it finds a match or runs out of pages. For resold GPUs, review the transfer hold in the first rental guide.
This example can automatically book a live rental. Make sure your wallet has enough credit for the hold. It sets a $0.99 cap and requests a stop right away. You may still be charged for billable time before the stop.
Save the run ID and original quote ID. If booking times out, retry only the booking request with the same quote ID, API key and idempotency key. If the stop fails, retry it for the same rental with the original stop key. Confirm the rental ended and any transfer settlement finished before treating the receipt as final.
See the first rental guide for model fit, funding and retry details.
#!/usr/bin/env bash
set -euo pipefail
: "${X9_API_URL:?Set X9_API_URL to your X9 endpoint}"
: "${X9_API_KEY:?Set X9_API_KEY to the capped renter key}"
K=(-H "Authorization: Bearer $X9_API_KEY")
: "${X9_RUN_ID:?Set and save a unique ID for this rental attempt}"
page=()
while true; do
offers=$(curl -fsS -G "$X9_API_URL/v1/offers" "${page[@]}" \
--data-urlencode 'source=x9' --data-urlencode 'gpu_class=consumer' \
--data-urlencode "min_vram_gb=16" --data-urlencode 'max_price_hr=0.99' \
--data-urlencode 'sort=price')
offer_id=$(jq -r 'first(.offers[] | select(.bookable and .gpu_count == 1)
| .offer_id) // ""' <<< "$offers")
if [[ -n "$offer_id" ]]; then break; fi
cursor=$(jq -r '.next_cursor // ""' <<< "$offers")
if [[ -z "$cursor" ]]; then
echo 'No matching bookable single GPU. No rental was made.' >&2
exit 1
fi
page=(--data-urlencode "cursor=$cursor")
done
quote_body=$(jq -nc --arg id "$offer_id" \
'{offer_id:$id,image:"ubuntu:24.04",max_runtime_s:3600,max_spend_usd:"0.99"}')
quote=$(curl -fsS "${K[@]}" "$X9_API_URL/v1/quotes" \
-H 'Content-Type: application/json' -H "Idempotency-Key: $X9_RUN_ID-quote" \
-d "$quote_body")
jq '{quote_id,price_hr_all_in,worst_case_usd,expires_in_s}' <<< "$quote"
quote_id=$(jq -er 'select((.worst_case_usd | tonumber) <= 0.99) | .quote_id' <<< "$quote")
printf 'Quote ID %s\n' "$quote_id" >&2
rental_body=$(jq -nc --arg id "$quote_id" '{quote_id:$id}')
rental=$(curl -fsS "${K[@]}" "$X9_API_URL/v1/instances" \
-H 'Content-Type: application/json' -H "Idempotency-Key: $X9_RUN_ID-rent" \
-d "$rental_body")
resource_id=$(jq -er '.resource_id' <<< "$rental")
printf 'Rental ID %s\n' "$resource_id"
# This example stops right away. Save files before stopping a real workload.
curl -fsS "${K[@]}" "$X9_API_URL/v1/resources/$resource_id/actions" \
-H 'Content-Type: application/json' -H "Idempotency-Key: $X9_RUN_ID-stop" \
-d '{"action":"stop"}' | jq '{status,final_cost_usd,settlement_pending}'
# Allow up to a minute here. If settlement takes longer, keep polling this resource.
for (( check=0; check<5; check++ )); do
resource=$(curl -fsS "${K[@]}" "$X9_API_URL/v1/resources/$resource_id")
if jq -e '(.status == "stopped" or .status == "failed" or .status == "host_lost"
or .status == "succeeded" or .status == "cancelled")
and (.settlement_pending != true)' <<< "$resource" > /dev/null; then
break
fi
if (( check == 4 )); then
echo "No final receipt yet. Check GET /v1/resources/$resource_id again before reading the final receipt." >&2
exit 1
fi
sleep "$(jq -r '.poll_after_s // 15' <<< "$resource")"
done
curl -fsS "${K[@]}" "$X9_API_URL/v1/receipts/$resource_id" \
| jq -e 'select(.extras == null or .extras.settled == true)
| {resource_id,status,price_hr,total_usd,line_items,extras}'