Research preview. Apply for access. We review each application and email you when it's approved, with your API key ready on the API keys page.

Submit and poll a completion job

You submit a completion once and fetch the result later. If the connection drops, you run the script again with the same idempotency key and get the same job back.

Run it: curl -O https://infra.acsresearch.org/examples/python_completion_jobs.py && ACS_API_KEY="acs-bm-..." python python_completion_jobs.py --model llama-8b --idempotency-key my-experiment-item-001

Prerequisites

  • You need an API key. Account explains where to create one.
  • You need Python 3.11 or later and httpx, which you install with pip install httpx.

Run the script

  1. Download python_completion_jobs.py.
  2. Set ACS_API_KEY, and set ACS_API_BASE only to call another deployment. Then run the script:
python python_completion_jobs.py \
  --model llama-8b --prompt 'Hello' --max-tokens 128 \
  --idempotency-key my-experiment-item-001 --deadline 900

When the job ends, the script prints the finished completion as one JSON line.

Script walkthrough

  1. It builds the request body from --model, --prompt, and --max-tokens. To change the completion, add fields such as n, seed, or echo to that body.
  2. It sends the body to POST /v1/completion-jobs with --idempotency-key in the Idempotency-Key header.
  3. It polls the job's status URL once a second until the job is no longer queued or running, then fetches the result URL.
  4. It stops polling after --deadline seconds, but the job keeps running on the server.
  5. It checks the HTTP status of the result before it prints the completion, because a finished job can end with an error status such as 429.

Keep the same idempotency key, API key, and request parameters when you rerun the script for the same experiment item.

Next steps