How to Add AI Video Generation to a LangChain Agent (2026)
langchain-treza is a Python package of LangChain tools that make pictures and video clips on Treza and run your saved Treza pipelines. This guide covers installing it, the one call we ran live, wiring the toolkit into an agent, the price cap that lives in your code instead of the prompt, and what comes back when a video takes longer than the tool waits.

To add AI video generation to a LangChain agent, install the langchain-treza package, set a Treza API key, and give the agent TrezaToolkit(max_cost_usd=...).get_tools(). The agent can then call treza_generate_video or treza_generate_image and gets back JSON with the finished file's link, the price and the model. The cap is set in your code, so the model cannot raise it.
Treza is a video and image pipeline platform; langchain-treza is our package that exposes it as LangChain tools. Treza is independent of LangChain and is not endorsed by it. The setup steps also live on the LangChain integration page, which is the short version of this guide.
What the package gives an agent
The package is on PyPI as langchain-treza, version 0.1.0 as of November 2026, MIT licensed, for Python 3.10 or newer with langchain-core 0.3 or newer. It has five tools. Each one is a normal LangChain BaseTool, so it works anywhere LangChain or LangGraph accepts tools.
| Class | Name the model sees | What it does | Cost |
|---|---|---|---|
TrezaGenerateImage | treza_generate_image | A picture from a prompt, optionally from reference pictures | Treza credits |
TrezaGenerateVideo | treza_generate_video | A clip from a prompt, optionally opening or ending on a picture | Treza credits |
TrezaRunPipeline | treza_run_pipeline | Runs one of your saved Treza pipelines with new inputs | Treza credits |
TrezaGetRun | treza_get_run | Looks up a render that was still going when its tool stopped waiting | Free |
TrezaBalance | treza_balance | The credits left on the account | Free |
The rendering happens on Treza's servers, so the machine running your agent needs no GPU and no model weights. The tools send the prompt and settings over HTTPS with your API key and wait for the finished file.
Step 1: Install it and add a key
Install the package into the environment your agent runs in:
pip install -U langchain-trezaThen make a key on the API keys page in your Treza account with the pipelines:read, pipelines:write and pipelines:run permissions. Keys start with treza_live_. Set it as an environment variable:
export TREZA_API_KEY="treza_live_..."You can pass api_key= when you build a tool instead. If neither is there, building the tool fails with "Pass api_key or set TREZA_API_KEY." and a link to the keys page. Renders are paid from prepaid Treza credits; the package itself is free and there is no subscription.
Step 2: Call one tool before you give it to an agent
Call a tool directly first. It proves the key works and shows you exactly what the agent will see, without a model in the loop. This is the call we ran live on 2026-11-19 against the published 0.1.0 package:
from langchain_treza import TrezaGenerateImage
tool = TrezaGenerateImage()
print(tool.invoke({"prompt": "A paper-cut lighthouse on a cliff at dusk", "aspect_ratio": "4:5"}))It finished in about 12 seconds and printed one line of JSON with url, status, finished, price_usd, model, render_seconds, run_id and pipeline_id. The model was google/gemini-2.5-flash-image (Nano Banana) and the price was $0.00, because Treza gives every account a few free pictures a day on its default image model. url is the link to the finished picture, ready to hand to the next step.
treza_generate_image takes prompt, aspect_ratio (1:1 by default; also 16:9, 9:16, 4:5, 3:4, 4:3, 2:3, 3:2), reference_image_urls and model. Leave model out to get Treza's default.
treza_generate_video takes prompt, aspect_ratio (16:9 by default, 9:16 or 1:1), duration_sec (4 seconds when left out), with_audio (on when left out, where the model can make sound), first_frame_url, last_frame_url and model. A picture the image tool made can be the first_frame_url of a clip, which is how you keep a character or a product looking the same from still to motion.
What a clip costs depends on the model and the length. Treza's default text to video model is Veo 3.1 Lite, and a 4 second 720p clip on it is quoted at $0.28. Other models cost more per second, which is why the next step matters.
Step 3: Set the price cap in code
Every render tool takes max_cost_usd when you build it. With a cap set, Treza prices the render first. If the price is higher than the cap, nothing starts, nothing is spent, and the tool returns:
This render costs $0.28, over the Max cost of $0.10. Nothing was spent. Raise Max cost, or ask for less (a shorter clip or a cheaper model).
The numbers in that message are the render's quote and your cap. This text is how the package words the refusal; we saw the same refusal come back live from Treza through our Vercel AI SDK package.
Three details make the cap safe to hand to an agent:
- The model cannot see or change it.
max_cost_usd, the API key and the wait are fields on the tool object, not arguments in the tool's schema. Whatever the prompt says, the cap you wrote is the cap. - No cap means no cap. The default is
None, and then a render runs at its quoted price as long as the balance covers it. Set one on every tool an agent can call. - The balance is checked too. A render never starts when the balance does not cover its price; the tool says what it costs, what the balance is, and where to add credits.
By default these refusals go back to the model as text, so the agent can react: pick a shorter clip, ask you, or stop. Pass handle_tool_error=False if you would rather they raise ToolException in your code.
Step 4: Give the tools to an agent
TrezaToolkit builds every tool with one key and one cap. This is how you would wire it into a LangChain agent, following the package's README. We have not run this agent sample with a real model, so we show no output for it:
from langchain.agents import create_agent
from langchain_treza import TrezaToolkit
tools = TrezaToolkit(max_cost_usd=1.00).get_tools()
agent = create_agent("anthropic:claude-sonnet-5-5", tools)
agent.invoke({"messages": [{"role": "user", "content": "Make a 4 second vertical clip of a fox in snow."}]})The model string is whatever chat model you already use with LangChain. If the agent should only generate and never run your saved pipelines, build the toolkit with TrezaToolkit(include_pipelines=False), which leaves out treza_run_pipeline.
In a LangGraph graph you do not need a model to decide anything. A node can call TrezaGenerateImage or TrezaGenerateVideo directly with .invoke(), exactly as in Step 2, between a node that writes the prompt and a node that posts or stores the result.
Step 5: Handle videos that outlast the wait
Pictures usually take under a minute. Videos take one to five minutes, longer than many agent loops want to block. Each tool waits up to wait_seconds: 300 by default, and 600 for treza_run_pipeline. Set it when you build the tool, next to max_cost_usd; like the cap, the model cannot change it.
When a render is still going after the wait, the tool does not raise and does not start it again. It returns JSON with "status": "running", "finished": false, and the run_id and pipeline_id. The render keeps going on Treza's side. Later, treza_get_run with those two IDs returns the same JSON shape with the file's url once it is done. Checking is free.
That gives you two patterns:
- Let the agent wait. Keep the default 300 seconds and most clips come back finished in one call.
- Let the agent move on. Use a short
wait_seconds, store the IDs, and have the agent (or your own code) calltreza_get_runon a later turn.
A render that breaks or is cancelled comes back as a refusal with the reason and a link to the pipeline in Treza, so you can see which step stopped.
Running your own pipelines as a tool
treza_run_pipeline is the reason to use Treza instead of wiring a single model API. A pipeline you built in Treza (a script, narration, several clips, captions, music, a 9:16 export) becomes one tool. The agent passes the pipeline's ID, which starts with pipeline_, and an inputs object with one entry per input step, for example {"prompt_1": "a lighthouse at dawn"}. It gets back the link to what the pipeline made, every output, and the price. The same max_cost_usd applies to the whole run.
If you have not built a pipeline yet, the AI video generator is where most people start: describe the video in chat, then save what worked as a pipeline an agent can run.
What goes wrong and how to fix it
- "Pass api_key or set TREZA_API_KEY." The key is missing in the process that builds the tool. Export it in that shell, or pass
api_key=. - A refusal that says to check your API key. The key was deleted, mistyped, or lacks the run permission. Make a new one with all three pipeline permissions.
- Every video is refused as over the Max cost. Your cap is below the model's quote. Raise
max_cost_usd, ask for a shorter clip, or name a cheaper model. - The agent keeps getting
"status": "running". The wait is shorter than the render. Raisewait_seconds, or make sure the agent hastreza_get_runand knows to use it. - A refusal about your balance. Add credits on the page the message links to; nothing was spent on the refused render.
For the same tools without Python, the video generation MCP server guide connects an MCP client to Treza directly. To set up this package, start from the LangChain integration page.
Frequently Asked Questions
Can a LangChain agent generate videos?
Yes. With langchain-treza installed and a Treza API key set, an agent gets a treza_generate_video tool that makes a clip from a prompt and returns the finished MP4's link, the price and the model. The rendering runs on Treza's servers, not on the machine running the agent.
How do I stop a LangChain agent from overspending on video?
Set max_cost_usd when you build the tools, for example TrezaToolkit(max_cost_usd=1.00). Treza prices each render first and refuses anything over the cap with nothing spent. The cap is not part of the tool's schema, so the model cannot change it.
Does it work with LangGraph?
Yes. The tools are standard LangChain tools, so a LangGraph agent can call them, and a graph node can call one directly with .invoke() when no model needs to decide.
What happens when a video takes longer than the tool waits?
The tool returns "status": "running" with a run_id and pipeline_id instead of an error, and the render keeps going. treza_get_run with those IDs returns the file once it is finished. Nothing is started twice.
Do I need a GPU to generate video from LangChain?
No. The tools call Treza's API and wait for the result, so your agent can run on a laptop or a small server. Only the API key and network access are needed.
How much does it cost?
The package is free. Renders use prepaid Treza credits with no subscription. A 4 second 720p clip on Veo 3.1 Lite, Treza's default video model, is quoted at $0.28, and each account gets a few free pictures a day on the default image model.
Is langchain-treza made by LangChain?
No. It is a package from Treza Labs, which is independent of LangChain and not endorsed by it. The source is public at github.com/treza-labs/langchain-treza.


