Integration guide

AgentGen + CrewAI

CrewAI orchestrates teams of agents working on complex tasks. AgentGen gives those crews the ability to produce tangible deliverables — a PDF report, a branded invoice, a certificate — and hand the download link to the user as the final output.

Install

pip install crewai crewai-tools requests python-dotenv

Step 1 — Define AgentGen as a CrewAI tool

CrewAI tools are subclasses of BaseTool. Implement one for PDF generation and one for images:

import os, requests
from crewai.tools import BaseTool
from pydantic import BaseModel, Field
from typing import Type

AGENTGEN_KEY = os.environ["AGENTGEN_API_KEY"]

class PdfInput(BaseModel):
    html: str = Field(description="Complete HTML document to render as PDF")
    page_size: str = Field(default="A4", description="Paper size: A4, Letter, A3, or Legal")

class GeneratePdfTool(BaseTool):
    name: str = "generate_pdf"
    description: str = (
        "Render an HTML document to a PDF and return a CDN download URL. "
        "Cost: 2 tokens per page. Use for invoices, reports, and certificates."
    )
    args_schema: Type[BaseModel] = PdfInput

    def _run(self, html: str, page_size: str = "A4") -> str:
        r = requests.post(
            "https://www.agent-gen.com/api/v1/generate/pdf",
            headers={"X-API-Key": AGENTGEN_KEY, "Content-Type": "application/json"},
            json={"html": html, "format": page_size},
            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()["url"]

class ImageInput(BaseModel):
    html: str = Field(description="HTML to render as an image")
    width: int = Field(default=1200)
    height: int = Field(default=630)

class GenerateImageTool(BaseTool):
    name: str = "generate_image"
    description: str = (
        "Render HTML to a PNG image and return a CDN URL. "
        "Cost: 1 token. Use for OG images, thumbnails, social cards."
    )
    args_schema: Type[BaseModel] = ImageInput

    def _run(self, html: str, width: int = 1200, height: int = 630) -> str:
        r = requests.post(
            "https://www.agent-gen.com/api/v1/generate/image",
            headers={"X-API-Key": AGENTGEN_KEY, "Content-Type": "application/json"},
            json={"html": html, "width": width, "height": height, "format": "png"},
            timeout=30,
        )
        r.raise_for_status()
        return r.json()["url"]

Step 2 — Create the agents

from crewai import Agent, Crew, Task
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o", temperature=0)
pdf_tool = GeneratePdfTool()
image_tool = GenerateImageTool()

# Agent that writes and renders documents
document_agent = Agent(
    role="Document Specialist",
    goal="Produce professional PDF documents from structured data.",
    backstory=(
        "You are an expert at creating beautiful, well-structured documents. "
        "You write clean HTML with inline styles and use tools to render them as PDFs."
    ),
    tools=[pdf_tool, image_tool],
    llm=llm,
    verbose=True,
)

# Agent that analyses data before handing off to document_agent
analyst_agent = Agent(
    role="Data Analyst",
    goal="Analyse business data and extract key insights for reporting.",
    backstory="You transform raw data into clear summaries and structured findings.",
    llm=llm,
    verbose=True,
)

Step 3 — Define tasks and run the crew

analyse_task = Task(
    description=(
        "Analyse these weekly metrics and produce a structured summary:
"
        "- Active Users: 12,847 (+8.3%)
"
        "- Revenue: $48,200 (+12.1%)
"
        "- Churn Rate: 2.1% (-0.4%)
"
        "Return: executive summary, 3 highlights, 2 concerns."
    ),
    expected_output="JSON with summary, highlights, and concerns",
    agent=analyst_agent,
)

report_task = Task(
    description=(
        "Using the analysis from the previous task, create a styled PDF report. "
        "Include: a dark blue header, metric cards for each KPI, "
        "the executive summary, highlights in green, concerns in red. "
        "Use generate_pdf and return the download URL."
    ),
    expected_output="A PDF download URL (https://cdn.agent-gen.com/...)",
    agent=document_agent,
    context=[analyse_task],
)

crew = Crew(
    agents=[analyst_agent, document_agent],
    tasks=[analyse_task, report_task],
    verbose=True,
)

result = crew.kickoff()
print(result)  # → https://cdn.agent-gen.com/output/abc123.pdf

Common crew patterns

Researcher → Writer → Document

One agent researches a topic, another writes a structured analysis, a third renders it to PDF for delivery.

Data Analyst → Report Generator

An analyst agent reads CSV/JSON data and produces insights; a document agent renders the styled PDF report.

Invoice Crew

A conversation agent collects client details; a document agent produces and emails the PDF invoice.

Give your CrewAI crew tangible deliverables

50 free tokens on signup — no credit card required.