Integration guide

AgentGen + LangChain

Add PDF and image generation to any LangChain agent in under 10 lines of Python. Define AgentGen as a StructuredTool, attach it to your agent, and your LLM can produce downloadable files on demand — no extra infrastructure required.

Install dependencies

pip install langchain langchain-anthropic langchain-openai requests python-dotenv

Quick start — @tool decorator

import os, requests
from langchain_core.tools import tool

@tool
def generate_pdf(html: str) -> str:
    """Generate a PDF from an HTML string. Returns a CDN download URL. Costs 2 tokens/page."""
    r = requests.post(
        "https://www.agent-gen.com/api/v1/generate/pdf",
        headers={"X-API-Key": os.environ["AGENTGEN_API_KEY"],
                 "Content-Type": "application/json"},
        json={"html": html, "format": "A4"},
        timeout=30,
    )
    r.raise_for_status()
    return r.json()["url"]

@tool
def generate_image(html: str) -> str:
    """Render HTML to a 1200x630 PNG. Returns a CDN URL. Costs 1 token."""
    r = requests.post(
        "https://www.agent-gen.com/api/v1/generate/image",
        headers={"X-API-Key": os.environ["AGENTGEN_API_KEY"],
                 "Content-Type": "application/json"},
        json={"html": html, "width": 1200, "height": 630, "format": "png"},
        timeout=30,
    )
    r.raise_for_status()
    return r.json()["url"]

Production — StructuredTool with Pydantic

from pydantic import BaseModel, Field
from langchain_core.tools import StructuredTool
from typing import Literal

class PdfInput(BaseModel):
    html: str = Field(description="Complete HTML document to render as PDF")
    format: Literal["A4", "Letter", "A3", "Legal"] = Field(default="A4")

generate_pdf_tool = StructuredTool.from_function(
    func=lambda html, format="A4": _call_api(
        "generate/pdf", {"html": html, "format": format}
    ),
    name="generate_pdf",
    description="Render HTML to a PDF and return a download URL. Cost: 2 tokens/page.",
    args_schema=PdfInput,
)

def _call_api(endpoint: str, payload: dict) -> dict:
    r = requests.post(
        f"https://www.agent-gen.com/api/v1/{endpoint}",
        headers={"X-API-Key": os.environ["AGENTGEN_API_KEY"],
                 "Content-Type": "application/json"},
        json=payload, timeout=30,
    )
    if r.status_code == 402:
        return {"error": "Insufficient tokens. Top up at agent-gen.com/pricing."}
    r.raise_for_status()
    return r.json()

Use with a tool-calling agent (Claude)

from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate

llm = ChatAnthropic(model="claude-opus-4-6", temperature=0)
tools = [generate_pdf_tool, generate_image]

prompt = ChatPromptTemplate.from_messages([
    ("system",
     "You generate professional PDFs and images on request. "
     "Write clean, styled HTML with inline styles, then call the appropriate tool."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

executor = AgentExecutor(
    agent=create_tool_calling_agent(llm, tools, prompt),
    tools=tools,
    verbose=True,
)

result = executor.invoke({
    "input": "Create a one-page PDF report titled 'Q1 2026 Summary' with revenue $1.2M"
})
print(result["output"])
# → "Here's your report: https://cdn.agent-gen.com/output/abc123.pdf"

Use with a ReAct agent (OpenAI)

from langchain import hub
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o", temperature=0)
prompt = hub.pull("hwchase17/react")

executor = AgentExecutor(
    agent=create_react_agent(llm, tools, prompt),
    tools=tools,
    verbose=True,
    handle_parsing_errors=True,
)

result = executor.invoke({"input": "Generate an OG image for a post titled 'AI Agents in 2026'"})
print(result["output"])

2 tokens/page

PDF cost

1 token

Image cost

< 2 seconds

Latency

For a full walkthrough including error handling, retry logic, and batch generation, see the complete LangChain integration guide.

Add document generation to your LangChain agent

50 free tokens on signup. No credit card required.