AI-generated video is moving from a novelty into a production tool, and developers building AI Agents are increasingly expected to wire up video generation the same way they'd wire up any other backend service: asynchronously, with proper error handling, and with cost controls baked in from the start. This guide walks through a practical integration pattern for the Seedance 2.5 API, covering everything from choosing the right generation route to letting an AI Agent own the full request-to-result lifecycle.
Choosing the right generation route
Before writing any integration code, the first decision is which generation mode fits the task:
- Text-to-Video (T2V) — the request is built entirely from a text prompt. This is the right choice when there's no source visual asset and the Agent (or user) is describing the desired scene from scratch.
- Reference-to-Video (R2V) — the request includes a video reference as an input asset. This is the right choice when the workflow needs to extend, adapt, or transform existing footage rather than generate a scene from nothing.
This choice matters beyond just output quality — it also determines how the request is billed, which is covered in the cost section below. A good pattern is to have your request-builder function make this decision explicitly (based on whether a reference asset is present) rather than leaving it implicit, since it changes both the payload shape and the cost calculation downstream.
Submitting an asynchronous video generation request
Video generation is not instant, so the API is designed around an asynchronous task model rather than a synchronous request/response. The typical flow looks like this:
- Build the request payload — prompt, mode (T2V or R2V), resolution, duration, and reference asset if applicable.
- Submit the request to the generation endpoint.
- Receive a task ID in response. This ID is your handle for everything that follows — store it immediately, ideally in the same record where you're tracking the Agent's overall job state.
Because the task ID is the only link between your submitted request and the eventual result, treat it as a first-class piece of state. If your Agent framework maintains a job or conversation state object, the task ID should be persisted there, not just held in a local variable that could be lost on a process restart.
Checking progress: polling vs. HTTPS callback
Once a task is submitted, there are two ways to find out when it's done:
- Polling — periodically query the task status endpoint using the stored task ID. This is simpler to implement and works well for lower-volume workflows or synchronous-feeling agent interactions where the Agent is expected to wait and report back.
- HTTPS callback — provide a callback URL when submitting the task, and let the service notify your backend when the task completes. This scales better for higher-volume pipelines, since it avoids the overhead of repeated status checks and lets your backend stay idle until there's actually something to process.
For an AI Agent workflow specifically, a hybrid approach often works best: use the callback as the primary completion signal, with a backup poll (e.g., a periodic sweep of "still pending" tasks) to catch any missed callback deliveries.
Handling validation errors, failed tasks, timeouts, and retries
Production-grade integrations need to account for several distinct failure modes, and they shouldn't all be handled the same way:
- Validation errors — the request payload itself is malformed or missing required fields. These should fail fast, before a task is even submitted, and be surfaced clearly so the Agent (or the human it's assisting) can correct the input rather than silently retrying a request that will never succeed.
- Failed tasks — the task was accepted but generation failed during processing. Your Agent should inspect the failure reason where available and decide whether a retry is likely to succeed or whether the request needs to be modified first.
- Timeouts — the task is taking longer than expected to complete, or a status check itself times out. Distinguish between "still processing" and "actually stuck" by tracking elapsed time against a reasonable ceiling, rather than treating every slow response as a failure.
- Retries — build retry logic with backoff, and cap the number of attempts. An Agent that retries indefinitely on a persistently failing request will quietly burn through budget without ever surfacing the underlying problem to a human.
Securely storing the API key
The API key should live in a backend environment variable, never in frontend code, client-side JavaScript, or embedded directly in a prompt that gets passed to an LLM. A few practical guidelines:
- Load the key from environment configuration (.env, secrets manager, or your platform's equivalent) at server startup, not from a hardcoded string.
- Keep the key out of logs — make sure your request-logging middleware redacts authorization headers.
- If your AI Agent has tool-calling access to the video generation function, the tool implementation should read the key from the environment internally; the Agent itself should never see or handle the raw key value as part of its reasoning or prompt context.
Letting the AI Agent own the workflow
With the pieces above in place, an AI Agent can be given end-to-end responsibility for the video generation step of a larger task:
- Prepare — the Agent interprets the user's goal and builds a well-formed request (mode, prompt or reference, resolution, duration).
- Submit — the Agent calls the submission function, which returns a task ID.
- Monitor — the Agent (or the backend orchestrating it) checks status via polling or callback until the task resolves.
- Return — once complete, the Agent retrieves the generated video and returns it as part of its response, along with any relevant metadata (duration, resolution, mode used).
This pattern keeps the Agent's reasoning focused on what to generate and how to react to outcomes, while the actual API mechanics stay in dedicated, testable functions the Agent calls as tools.
Estimating generation costs based on mode, duration, and resolution
Cost estimation should happen before submission, not after — an Agent that can estimate cost up front can make better decisions about resolution and duration, and can flag expensive requests for human confirmation before spending budget. The Seedance 2.5 API price structure differs depending on which generation route is used, which is worth building directly into your cost-estimation function:
Text-to-Video, billed by output duration:
- 480p: USD 0.138 per output second
- 720p: USD 0.296 per output second
Reference-to-Video, when a video reference is used, billed by input plus output duration:
- 480p: USD 0.084 per input + output second
- 720p: USD 0.180 per input + output second
The key distinction to encode in your cost function: T2V cost is a straightforward function of output length and resolution, while R2V cost also factors in the length of the reference input being processed. A request-cost estimator that doesn't account for this difference will systematically misquote R2V jobs, especially as reference asset length grows. For an Agent making autonomous resolution or duration trade-offs, having this calculation available as a callable function (rather than embedded only in documentation) means it can weigh quality against budget in real time.
Putting it together
A solid integration boils down to a small set of well-separated concerns: a request builder that knows the difference between T2V and R2V, an async submission and monitoring layer that treats the task ID as durable state, error handling that distinguishes fixable input problems from transient failures, a key-storage pattern that never exposes credentials to the frontend or the Agent's prompt context, and a cost estimator that mirrors the actual billing model. Once those pieces exist as discrete, testable functions, an AI Agent can be given tool access to them and handle the full video generation lifecycle — from interpreting a goal to returning a finished asset — without a human needing to babysit each step.
