Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Build async video generation workflows with Kling AI using queues, state machines, and event-driven patterns. Trigger with phrases like 'klingai async', 'kling ai workflow', 'klingai pipeline', 'async video generation'.
.claude/skills/jeremylongshore-klingai-async-workflows/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -7% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 24% | 0% |
| case-11 | ✗→✓ | ▲ Improved | 135% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 32% | 0% |
| case-15 | ✗→✓ | ▲ Improved | 36% | 0% |
Kling AI video generation is inherently async: you submit a task, then poll or receive a callback when done. This skill covers production patterns for integrating this into larger systems using queues, state machines, and event-driven architectures.
pythonimport jwt, time, os, requests BASE = "https://api.klingai.com/v1" def get_headers(): ak, sk = os.environ["KLING_ACCESS_KEY"], os.environ["KLING_SECRET_KEY"] token = jwt.encode( {"iss": ak, "exp": int(time.time()) + 1800, "nbf": int(time.time()) - 5}, sk, algorithm="HS256", headers={"alg": "HS256", "typ": "JWT"} ) return {"Authorization": f"Bearer {token}", "Content-Type": "application/json"} def submit_async(prompt, callback_url=None, **kwargs): """Submit task and return immediately.""" body = { "model_name": kwargs.get("model", "kling-v2-master"), "prompt": prompt, "duration": str(kwargs.get("duration", 5)), "mode": kwargs.get("mode", "standard"), } if callback_url: body["callback_url"] = callback_url r = requests.post(f"{BASE}/videos/text2video", headers=get_headers(), json=body) return r.json()["data"]["task_id"]
pythonimport redis import json r = redis.Redis() # Producer: enqueue video generation requests def enqueue_video_job(prompt, metadata=None): job = { "id": f"job_{int(time.time() * 1000)}", "prompt": prompt, "metadata": metadata or {}, "status": "queued", "created_at": time.time(), } r.lpush("kling:jobs:pending", json.dumps(job)) return job["id"] # Worker: process jobs from queue def process_jobs(max_concurrent=3): active_tasks = {} while True: # Submit new jobs if under concurrency limit while len(active_tasks) < max_concurrent: raw = r.rpop("kling:jobs:pending") if not raw: break job = json.loads(raw) task_id = submit_async(job["prompt"]) active_tasks[task_id] = job r.hset("kling:jobs:active", task_id, json.dumps(job)) # Check active tasks completed = [] for task_id, job in active_tasks.items(): result = requests.get( f"{BASE}/videos/text2video/{task_id}", headers=get_headers() ).json() status = result["data"]["task_status"] if status == "succeed": job["status"] = "completed" job["video_url"] = result["data"]["task_result"]["videos"][0]["url"] r.lpush("kling:jobs:completed", json.dumps(job)) completed.append(task_id) elif status == "failed": job["status"] = "failed" job["error"] = result["data"].get("task_status_msg") r.lpush("kling:jobs:failed", json.dumps(job)) completed.append(task_id) for tid in completed: active_tasks.pop(tid) r.hdel("kling:jobs:active", tid) time.sleep(10)
pythonfrom enum import Enum from dataclasses import dataclass, field from typing import Optional class JobState(Enum): QUEUED = "queued" SUBMITTING = "submitting" PROCESSING = "processing" DOWNLOADING = "downloading" COMPLETED = "completed" FAILED = "failed" RETRYING = "retrying" @dataclass class VideoJob: prompt: str state: JobState = JobState.QUEUED task_id: Optional[str] = None video_url: Optional[str] = None error: Optional[str] = None attempts: int = 0 max_attempts: int = 3 def can_retry(self) -> bool: return self.state == JobState.FAILED and self.attempts < self.max_attempts def transition(self, new_state: JobState): valid = { JobState.QUEUED: {JobState.SUBMITTING}, JobState.SUBMITTING: {JobState.PROCESSING, JobState.FAILED}, JobState.PROCESSING: {JobState.DOWNLOADING, JobState.FAILED}, JobState.DOWNLOADING: {JobState.COMPLETED, JobState.FAILED}, JobState.FAILED: {JobState.RETRYING}, JobState.RETRYING: {JobState.SUBMITTING}, } if new_state not in valid.get(self.state, set()): raise ValueError(f"Invalid transition: {self.state} -> {new_state}") self.state = new_state
pythonasync def video_pipeline(prompt, steps=None): """Chain: generate -> extend -> download -> upload.""" steps = steps or ["generate", "extend", "download"] # Step 1: Generate task_id = submit_async(prompt, duration=5) result = poll_task("/videos/text2video", task_id) # from job-monitoring skill video_url = result["videos"][0]["url"] # Step 2: Extend (optional) if "extend" in steps: ext_r = requests.post(f"{BASE}/videos/video-extend", headers=get_headers(), json={ "task_id": task_id, "prompt": f"Continue: {prompt}", "duration": "5", }).json() ext_result = poll_task("/videos/video-extend", ext_r["data"]["task_id"]) video_url = ext_result["videos"][0]["url"] # Step 3: Download if "download" in steps: video_data = requests.get(video_url).content filepath = f"output/{task_id}.mp4" with open(filepath, "wb") as f: f.write(video_data) return filepath return video_url
python# Use callback_url to avoid polling entirely task_id = submit_async( "Sunset over ocean with sailboats", callback_url="https://your-app.com/webhooks/kling" ) # Your webhook handler triggers next pipeline step # See klingai-webhook-config skill for receiver implementation
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 35,262 | 24,132 | -32% | 1 | 1 | 0% | 6,252 | 5,834 | -7% | 0 | 0 | — |
case-02 | fail→fail | 19,573 | 17,496 | -11% | 1 | 1 | 0% | 4,184 | 4,626 | +11% | 0 | 0 | — |
case-03 | fail→pass | 25,911 | 24,017 | -7% | 1 | 1 | 0% | 4,356 | 5,407 | +24% | 0 | 0 | — |
case-04 | fail→fail | 18,929 | 12,999 | -31% | 1 | 1 | 0% | 2,516 | 4,234 | +68% | 0 | 0 | — |
case-05 | fail→fail | 49,651 | 19,872 | -60% | 1 | 1 | 0% | 1,506 | 5,965 | +296% | 0 | 0 | — |
case-06 | fail→fail | 20,768 | 19,350 | -7% | 1 | 1 | 0% | 2,515 | 4,911 | +95% | 0 | 0 | — |
case-07 | fail→fail | 15,098 | 14,292 | -5% | 1 | 1 | 0% | 2,117 | 3,703 | +75% | 0 | 0 | — |
case-08 | fail→fail | 11,572 | 9,168 | -21% | 1 | 1 | 0% | 2,516 | 3,775 | +50% | 0 | 0 | — |
case-09 | pass→pass | 18,365 | 8,795 | -52% | 1 | 1 | 0% | 2,938 | 3,701 | +26% | 0 | 0 | — |
case-10 | fail→fail | 17,593 | 5,340 | -70% | 1 | 1 | 0% | 2,190 | 2,699 | +23% | 0 | 0 | — |
case-11 | fail→pass | 12,812 | 6,118 | -52% | 1 | 1 | 0% | 1,095 | 2,575 | +135% | 0 | 0 | — |
case-12 | pass→pass | 9,772 | 9,612 | -2% | 1 | 1 | 0% | 1,395 | 2,455 | +76% | 0 | 0 | — |
case-13 | pass→pass | 9,701 | 11,161 | +15% | 1 | 1 | 0% | 1,894 | 2,716 | +43% | 0 | 0 | — |
case-14 | fail→pass | 19,721 | 5,462 | -72% | 1 | 1 | 0% | 1,921 | 2,538 | +32% | 0 | 0 | — |
case-15 | fail→pass | 15,958 | 9,733 | -39% | 1 | 1 | 0% | 1,876 | 2,558 | +36% | 0 | 0 | — |
case-16 | fail→pass | 27,686 | 7,748 | -72% | 1 | 1 | 0% | 3,670 | 3,310 | -10% | 0 | 0 | — |
case-17 | fail→pass | 17,360 | 3,145 | -82% | 1 | 1 | 0% | 2,190 | 2,258 | +3% | 0 | 0 | — |
case-18 | fail→pass | 12,353 | 10,245 | -17% | 1 | 1 | 0% | 2,155 | 2,575 | +19% | 0 | 0 | — |
case-19 | pass→pass | 9,303 | 2,568 | -72% | 1 | 1 | 0% | 1,627 | 2,247 | +38% | 0 | 0 | — |
case-20 | fail→pass | 19,475 | 3,107 | -84% | 1 | 1 | 0% | 1,819 | 2,324 | +28% | 0 | 0 | — |
case-21 | fail→pass | 15,924 | 3,974 | -75% | 1 | 1 | 0% | 2,038 | 2,540 | +25% | 0 | 0 | — |
case-22 | fail→pass | 14,267 | 7,866 | -45% | 1 | 1 | 0% | 1,324 | 2,170 | +64% | 0 | 0 | — |
case-23 | fail→pass | 14,220 | 6,043 | -58% | 1 | 1 | 0% | 1,652 | 2,620 | +59% | 0 | 0 | — |
case-24 | pass→pass | 47,316 | 8,178 | -83% | 1 | 1 | 0% | 2,898 | 3,314 | +14% | 0 | 0 | — |
case-25 | pass→pass | 24,637 | 14,755 | -40% | 1 | 1 | 0% | 2,547 | 3,940 | +55% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 25 cases were attempted, and 24 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +48 percentage points is the difference between those two pass rates over the 24 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.