Build a Custom AI Agent in Python: Automate Web Research & Email Reports

Build a Custom AI Agent in Python: Automate Web Research & Email Reports - AIinActionHub
3 min read 623 words
Last updated:
⏱ 1 min read May 16, 2026 By Theo Grant
Share: 𝕏 P f
Disclosure: AIinActionHub may earn a commission from qualifying purchases through affiliate links in this article. This helps support our work at no additional cost to you. Learn more.
Last updated: August 21, 2026



“`html

Build a Custom AI Agent in Python: Automate Web Research & Email Reports

What We’re Building & Why AI Agents Are a Game-Changer

  • We’re building a Python AI agent that takes a topic, searches the web, summarizes the top results, and emails a formatted report.
  • Understand the core loop: LLM reasoning (planning) -> Tool Execution (search) -> LLM Synthesis (understanding) -> Action (email).
  • Practical applications: Automate competitor analysis, generate daily news digests, or monitor brand mentions without manual effort.

Prerequisites: Setting Up Your AI Toolbox

Stay in the loop

Get the latest insights delivered straight to your inbox.

  • Python 3.10+ installed, along with a virtual environment to manage dependencies.
  • API keys: An OpenAI API key (for the LLM brain) and a SerpAPI key (for web search). Store them in a .env file for security.
  • Core libraries installed via pip: openai, serpapi, python-dotenv.
1

Defining the Agent’s “Hands” (Tools and Functions)

  • Create a web_search(query) function using the SerpAPI library to fetch the top 5 search results for a given query.
  • Create a summarize_content(text, topic) function using OpenAI’s gpt-4o-mini model to condense the search results into a concise summary.
  • Create a send_email_report(recipient, subject, body) function using SMTP or SendGrid to deliver the final summary to a specified inbox.
2

Building the “Brain” (The Orchestration Loop)

  • Design the main agent_loop(user_query) function, starting with a system prompt that clearly defines the available tools and their JSON schema.
  • Implement structured output: The LLM must return a JSON action (e.g., {"tool": "web_search", "parameters": {"query": "latest AI news"}}) or a final answer.
  • Write the loop logic: Parse the JSON action -> Execute the matching Python function -> Feed the result back to the LLM for the next step, repeating until a final answer is reached.
3

Execution, Testing, and Error Handling

  • Run the script with python agent.py and test with a query like “Find the latest trends in generative AI for healthcare”.
  • Add robust error handling: try/except blocks for API timeouts, invalid JSON parsing from the LLM, and missing environment variables.
  • Implement logging (using the logging library) to print the agent’s internal “thought” process for debugging and transparency.

Future Enhancements: From Prototype to Production

Featured on
Listed on DevTool.io Listed on SaaSHub

Enjoyed this article?

Join thousands of readers who get our best insights delivered weekly. Free, no spam, unsubscribe anytime.

Subscribe Free →
Scroll to Top