26. CLI Automation Pipeline
Script batch generation, cost gates, retries, and output handling with the CLI's JSON output and job IDs, and decide when to move to the API.
Written and reviewed by Eric Han · Co-founder & CTO, reopt
Key takeaways
- The backbone of automation is four steps: check cost, submit without waiting and save job IDs, wait for and look up each ID, then download results and record them in a manifest. All of it can be built with official CLI commands alone.
generate costtakes the same arguments, calculates only credits, and creates no job. A cost gate that compares the whole batch's total against the budget is the first safeguard against accidents.- The default wait for
--waitis 10 minutes. When it runs out, only the command ends and the job keeps running on the server, so do not resubmit; continue checking withgenerate get. - In the enterprise help center table, Seedance 2.5 is billed per second. A 480p draft costs 3 credits per second and 720p costs 7, so a ladder that picks from drafts and renders only approved cuts at high resolution cuts costs sharply.
- CLI login uses browser approval and tokens are short-lived. According to user reports, running it daily in unattended CI is still hard. Move unattended server jobs to the Higgsfield API, which uses API keys and webhooks.
When you make only a few items at a time, generating in a conversation is easier. But to make dozens of variants under the same rules, total the cost in advance, and save results under a naming scheme, a script works better. This chapter covers building such a pipeline with the official CLI. Installation, login, and basic commands are in Chapter 6, so here we look only at the patterns automation needs.
The scripts below use only commands and flags from the official README, the official CLI blog (published 2026-10-19, tested on 2026-10-16 with CLI 1.1.26), and the cookbook in the official skills repository. Where the official docs do not give an output format, a comment says so. Run a single item first to check the output shape, then run the batch.
The Backbone of Automation
The whole flow splits into four steps. First total the cost and stop if it exceeds the budget. If it passes, submit without waiting and collect only job IDs. Wait for or look up the collected IDs one by one to get results. Finally, download the result files under the naming scheme and record them in a manifest.
Splitting it this way lets you stop and restart at each step. Because the job ID file remains, you can rerun just the collection step. The submission, which costs money, happens only once.
Job Status and Waiting
The CLI's --wait polls server status until the job finishes. The default polling interval is 3 seconds (--wait-interval) and the maximum wait is 10 minutes (--wait-timeout). In the official CLI blog's test, a text-only 1080p video took more than 10 minutes, and the command ended with "timed out waiting for job." The job kept running on the server: generate get showed in_progress, and the result link appeared a few minutes later.
A finished job is not always a success. The troubleshooting doc in the official skills repository treats failed, nsfw, and ip_detected as terminal states. The API docs define request status as queued, in_progress, completed, failed, nsfw, and canceled. The script treats only completed as success and records everything else in the failure list.
Script 1: Cost gate
higgsfield generate cost takes the same model ID, arguments, and media flags as generate create. It creates no job and only prints the price. The script below gets the cost for every line of the prompt file, totals it, and stops with a nonzero exit code if the budget is exceeded.
#!/usr/bin/env bash
# cost-gate.sh — Total the whole batch's credits in advance and stop if over budget.
# Usage: ./cost-gate.sh prompts.txt 600
set -euo pipefail
PROMPTS_FILE="$1"
BUDGET="$2" # Credits allowed for this batch
MODEL="seedance_2_5"
DURATION=5
RESOLUTION=720p
total=0
while IFS= read -r prompt; do
[ -z "$prompt" ] && continue
# Sample output per the official blog: "80 credits". The JSON field name is not in
# the official docs, so take the first number from the human-readable output.
# Check the output by eye the first time.
out=$(higgsfield generate cost "$MODEL" --prompt "$prompt" \
--duration "$DURATION" --resolution "$RESOLUTION")
credits=$(printf '%s' "$out" | grep -Eo '[0-9]+(\.[0-9]+)?' | head -n 1 || true)
if [ -z "$credits" ]; then
echo "Could not read cost. Output: $out" >&2
exit 2
fi
total=$(awk -v a="$total" -v b="$credits" 'BEGIN { print a + b }')
printf '%s\t%s\n' "$credits" "$prompt"
done < "$PROMPTS_FILE"
echo "Total: $total credits / Budget: $BUDGET credits"
if awk -v t="$total" -v b="$BUDGET" 'BEGIN { exit !(t > b) }'; then
echo "Over budget. Not submitting." >&2
exit 1
fiAccording to the official CLI README, the voice-change and dubbing workflows do not support cost estimation. If such jobs are mixed into a batch, the gate cannot compute their cost, so split them out and have a person approve them. Workflow cost uses the form generate cost workflow <name> ..., not generate workflow cost.
Script 2: Submit and Collect
The official CLI blog's batch example submits without --wait, collects job IDs in a file, and waits on each ID in a second loop. The scripts below add a manifest and a naming scheme to that structure. The naming scheme follows the yyyy-mm-dd-hh-mm-ss-name.ext format the official cookbook recommends.
#!/usr/bin/env bash
# submit.sh — Submit without waiting and record job IDs and prompts in a manifest.
# Usage: ./submit.sh prompts.txt (run after cost-gate.sh passes)
set -euo pipefail
PROMPTS_FILE="$1"
MODEL="seedance_2_5"
RUN_ID=$(date +%Y-%m-%d-%H-%M-%S)
mkdir -p "runs/$RUN_ID"
n=0
while IFS= read -r prompt; do
[ -z "$prompt" ] && continue
n=$((n + 1))
# With --json and no --wait, the output is a list of job IDs (a one-element array) (official CLI blog).
id=$(higgsfield generate create "$MODEL" --prompt "$prompt" \
--duration 5 --resolution 720p --json | jq -r '.[0]')
printf '%s\t%s\t%s\n' "$id" "shot-$n" "$prompt" >> "runs/$RUN_ID/jobs.tsv"
done < "$PROMPTS_FILE"
echo "Recorded $n jobs in runs/$RUN_ID/jobs.tsv"#!/usr/bin/env bash
# collect.sh — Wait for each job ID's result and download finished files under the naming scheme.
# Usage: ./collect.sh runs/2026-11-21-10-30-00
set -euo pipefail
RUN_DIR="$1"
mkdir -p "$RUN_DIR/out"
while IFS=$'\t' read -r id name prompt; do
# Raise the default 10-minute wait to 20 minutes. Past that, the job keeps running on the server.
rec=$(higgsfield generate wait "$id" --wait-timeout 20m --json 2>/dev/null \
|| higgsfield generate get "$id" --json)
# Take the first record whether the response is an array or an object.
status=$(printf '%s' "$rec" | jq -r 'if type=="array" then .[0] else . end | .status')
url=$(printf '%s' "$rec" | jq -r 'if type=="array" then .[0] else . end | .result_url // empty')
if [ "$status" = "completed" ] && [ -n "$url" ]; then
file="$RUN_DIR/out/$(date +%Y-%m-%d-%H-%M-%S)-$name.mp4"
curl -fsSL "$url" -o "$file"
printf '%s\t%s\t%s\t%s\n' "$id" "$status" "$file" "$prompt" >> "$RUN_DIR/manifest.tsv"
else
printf '%s\t%s\t\t%s\n' "$id" "$status" "$prompt" >> "$RUN_DIR/failed.tsv"
fi
done < "$RUN_DIR/jobs.tsv"The official CLI blog explains that the job record JSON contains id, job_type, status, result_url, and the parameters actually used. generate list --json shows the entire generation history, so it is not suited to collecting a single batch. Always collect a batch from its own ID file. The official cookbook also shows an example that saves results directly to a folder with the --output-dir flag. That flag is not in the README's common flags table, so confirm it with higgsfield generate create --help before use.
Script 3: Parallel Runs and Retries
The official cookbook's UGC ad recipe launches four modes at once with & and waits with wait. Set the parallel count so it does not exceed your plan's concurrent generation limit. In the pricing table, the Starter plan allows up to 2 videos and 4 images in parallel, and the free plan allows one at a time. Exceeding the limit causes the "You've hit your limit of generations at the same time" error the help center describes. If the server receives too many requests, you get the Higgsfield API error (HTTP 429) from the official troubleshooting doc; in that case, retry after a pause.
#!/usr/bin/env bash
# parallel.sh — Limit concurrent runs and retry 429 errors with growing intervals.
set -euo pipefail
MAX_PARALLEL=2 # Keep at or below your plan's concurrent video limit
MAX_RETRY=5
submit_with_retry() {
local prompt="$1" attempt=1 wait_s=15 out
while :; do
if out=$(higgsfield generate create seedance_2_5 --prompt "$prompt" \
--duration 5 --resolution 720p --json 2>&1); then
printf '%s\n' "$out" | jq -r '.[0]'
return 0
fi
# Retry only on 429. For other errors, fix the cause and rerun.
if printf '%s' "$out" | grep -q 'HTTP 429' && [ "$attempt" -lt "$MAX_RETRY" ]; then
sleep "$wait_s"; attempt=$((attempt + 1)); wait_s=$((wait_s * 2))
else
echo "Submission failed: $out" >&2
return 1
fi
done
}
while IFS= read -r prompt; do
[ -z "$prompt" ] && continue
while [ "$(jobs -rp | wc -l)" -ge "$MAX_PARALLEL" ]; do sleep 2; done
submit_with_retry "$prompt" >> job_ids.txt &
done < prompts.txt
waitThe rule for retries is never to submit the same job twice. A wait timeout is not a failure. Apply submission retries only to errors like 429, where it is certain no job was created. The CLI has no documented duplicate-prevention mechanism like the API's Idempotency-Key. After an ambiguous failure, use generate list to check whether a job with the same prompt already exists.
From Draft to Final Render
The enterprise help center's animated ad case records the prices shown on the generation screen by resolution and length. Both models are billed per output second, so cost rises in proportion to length. Sound on or off does not change the price. The chart below shows credits per second from that table. The 1080p values are discounted prices; the pre-discount values are in parentheses in the same table.
- Seedance 2.5 draft 480p3.00credits
- Seedance 2.5 720p7.00credits
- Seedance 2.5 1080p discounted11.20credits16 before discount
- Kling 4.0 720p discounted2.25credits2.75 before discount
- Kling 4.0 1080p discounted3.25credits4 before discount
Pulling the per-length values from the same table shows the gap between resolutions widening in proportion to length. From bottom to top, the lines are Seedance 2.5's 480p draft, 720p, and 1080p discounted price.
The draft ladder exploits this difference. The same case document explains that a Seedance 2.5 draft produces a 480p take that can be finalized to 1080p within 7 days. Finalizing re-renders and bills the draft's full length, so you pay the high-resolution cost only for approved takes. The finalization price itself is left as "not verified" even in the case document. The official docs also document no CLI command for finalizing a draft. On the CLI side, you can preview the draft price with the --draft true cost lookup the official blog showed. Under the same 5-second 1080p settings, the full render was 80 credits and the draft was 15.
# Cost comparison from the official CLI blog. No job is created.
higgsfield generate cost seedance_2_5 --prompt "..." --duration 5 --resolution 1080p
higgsfield generate cost seedance_2_5 --prompt "..." --duration 5 --resolution 1080p --draft trueWorkspaces and Tokens
A team account may have several billing workspaces. higgsfield workspace list lists workspaces with their plans and balances, and workspace select picks the workspace billed for generation. Checking the workspace on the first line of a script before running a batch prevents charges against the wrong budget. A user report (cli issue #99, CLI 1.1.26) describes a case where the workspace selection was not saved. After selecting, recheck the actual state with workspace list and account status.
When the batch finishes, look up recent credit movements with something like higgsfield account transactions --size 5 and compare them against the manifest. The same records appear under Manage Account → Usage on the web.
higgsfield auth token prints the current access token. The official CLI blog says to treat this token as a secret. Do not put it in prompts, logs, screenshots, or source repositories. The token is stored in a local credentials file and is short-lived. If you see "Session expired," run auth login again. On shared machines, clear the token with higgsfield auth logout when you are done.
Do not put tokens in scripts
Pasting auth token output into an automation script leaves account access in the repository or CI logs. Keep credentials only in the local file the CLI manages, and if you suspect a leak, rotate the token by logging out and back in.
CI Limits and When to Move to the API
CLI login uses browser approval. auth login opens a browser, and when the user allows it, a success message appears in the terminal. An unattended CI server cannot go through this step. According to a user report (cli issue #47, filed 2026-07-08, open at time of checking), the workaround of copying the credentials file into a CI secret does not hold up: the token expires in about a day, so you have to log in again every day. No official fix had been announced as of the check.
Unattended server jobs suit the Higgsfield API, a separate product. The flow below is this book's judgment based on the product distinctions in the official docs.
| Criterion | CLI script | Higgsfield API |
|---|---|---|
| Authentication | Browser login, short-lived token | Key ID and secret, kept only on the server |
| Billing | Higgsfield plan credits | Separate prepaid dollar balance (expires after 1 year) |
| Duplicate prevention | No documented mechanism | Idempotency-Key header |
| Completion notice | Repeated polling (--wait) | Polling or webhook |
| Concurrency overflow | Concurrent generation limit error, 429 | Queued; when the queue is full, 429 account_queue_full with Retry-After |
| Result retention | Kept in Assets | Request files deleted 7 days after completion |
| Model list | Same catalog as the web app | API-only catalog, may differ from the web |
After moving to the API, result files are deleted after 7 days, so the collection script must finish downloading and writing the manifest. API authentication, the request lifecycle, and webhooks are covered in detail in Chapter 18.
23. Variants and Experiments
How to organize batch-made variants into experiments
27. Team Operations Playbook
Turn script run permissions and budget approvals into team rules
Official Sources
25. Orchestrating Other Tools
Combine Higgsfield MCP with connectors like Canva, Notion, and Slack, creative app bridges, and website deployment into one flow from plan to publish.
27. Team Operations Playbook
Rules and a rollout plan for teams running Higgsfield MCP: requests, approvals, budgets, rights records, reusable prompts, metrics, and incidents.