How to use AI to generate reports automatically
Quick answer
Use a large language model like
gpt-4o to generate reports by providing structured prompts describing the report content. Automate this by calling the chat.completions.create API with your data and formatting instructions to receive a complete report text.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable to authenticate requests.
pip install openai>=1.0 Step by step
This example shows how to generate a sales report automatically by sending a prompt to gpt-4o. The prompt includes the report topic and data summary. The model returns a formatted report.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"Generate a detailed monthly sales report for April 2026. "
"Include total sales, top products, and recommendations for next month. "
"Data summary: Total sales $120,000, Top products: Widget A (40%), Widget B (35%), Widget C (25%)."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
report = response.choices[0].message.content
print(report) output
Monthly Sales Report - April 2026 Total Sales: $120,000 Top Products: - Widget A: 40% of sales - Widget B: 35% of sales - Widget C: 25% of sales Recommendations: 1. Increase inventory for Widget A and B due to high demand. 2. Launch targeted marketing campaigns for Widget C to boost sales. 3. Monitor sales trends closely for early detection of shifts. Overall, April showed strong performance with opportunities to optimize product focus for May.
Common variations
You can customize report generation by using different models like claude-3-5-sonnet-20241022 or by streaming output for real-time display. Async calls improve throughput for batch report generation.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
system_prompt = "You are a helpful assistant that generates detailed reports."
user_prompt = (
"Generate a quarterly financial report for Q1 2026 including revenue, expenses, and profit analysis."
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
print(message.content[0].text) output
Q1 2026 Financial Report Revenue: $350,000 Expenses: $200,000 Profit: $150,000 Analysis: The company maintained a healthy profit margin of approximately 43%. Revenue growth was driven by increased sales in the new product line. Expense control measures helped improve net profit compared to Q4 2025.
Troubleshooting
- If the report output is too brief or generic, provide more detailed context and data in your prompt.
- If you encounter API rate limits, implement exponential backoff or batch requests asynchronously.
- For formatting issues, specify the desired report structure explicitly in the prompt (e.g., bullet points, sections).
Key Takeaways
- Use structured prompts with clear data and instructions to get precise reports from AI models.
- Automate report generation by integrating
chat.completions.createcalls in your Python scripts. - Experiment with different models and async calls to optimize speed and output quality.
- Specify formatting in prompts to control report style and readability.
- Handle API limits gracefully with retries and batching for large-scale automation.