Automating with AI Just Got Easier
How I built an AI Email Research Agent with Cursor and Python
Learning how to code has been turned on its head since the advent of GenAI.
It used to be a slow process, and personally, it took me years to build confidence. In the past, you had to memorize syntax, dig through documentation, and spend countless hours googling to write and debug your code. Now, with AI assistance and basic Python skills, you can approach almost any problem and let AI code it for you right away.
Any researcher or analyst can quickly create highly customized automation tools and data analysis workflows without investing months or even years into learning Python.
In this post, I’ll cover how you can effectively utilize AI-powered code editors, specifically focusing on Cursor.
What we’ll cover:
What can AI code editors do?
A demonstration using Cursor to build a Gmail AI agent that searches your inbox for specific emails and generates a summary email for you.
Best practices for using Cursor effectively.
Subscribe and join 3k+ others to work smarter through automation and become a savvier analyst.
Consider upgrading to paid. It’s great motivation for me to keep investing time and energy into these posts. Extra perks include detailed code snippets for bigger projects.
How have AI editors changed the game?
There's been quite a bit of hype around an AI-powered code editor called Cursor.
People have been building some impressive things with it, even with very little coding experience. This person built a flight simulator video game in just a few hours and now earns five figures from ads alone.
AI code editors integrate the power of the most popular large language models directly into your coding environment. You no longer need to prompt an AI in an external chat window (like ChatGPT) and then copy-paste the generated code into your execution environment (Jupyter, VS Code, etc.).
Key features include in-line prompting and intelligent tab-completion that suggests code based on how you use it.
What makes these editors particularly powerful recently is the introduction of agent mode.
AI agents operate more autonomously and require less hand-holding compared to traditional back-and-forth prompting. They interact with their environment, gather context, and use available tools. With just one prompt, agents can create all the necessary files and maintain consistency throughout your entire project.
All the coding is done for you on the fly, meaning your main task is simply running and debugging.
Putting Cursor to the test: A Gmail agent
I wanted to put Cursor to the test by building an AI agent myself—something I have no prior experience with.
Specifically, I decided to build a Gmail agent that searches through my inbox and generates a bullet-point summary of emails from The Economist.
Cursor isn’t the only AI-powered editor on the market. Others, like GitHub Copilot, have been around for a while, although I find Cursor to be faster. It was easy to install and feels similar to VS Code, as it's built on top of it.
Here's the prompt I used for the gmail agent:
Note that I provided some context by uploading a Jupyter notebook template from another tutorial. Cursor quickly generated gmail_agent.py
, complete with all the correct imports and structure.
There were a few slight refinements I had to make, following the initial prompt:
I had to instruct the agent to ignore the drafts folder and only search the inbox.
I added an option to choose which OpenAI model to use.
Sending emails didn't initially work because I hadn't specified the email recipient clearly and needed to use the Gmail API directly to send it.
Overall, it took about two hours to get everything running. About an hour of this was actually spent setting up the Google API credentials themselves, which Cursor helped me with as well.
Here's the agent in action:
First, the agent searches my inbox for emails with a specific subject line (the Economist’s “Off the Charts” newsletter) and gathers context. Then, it analyzes each email, extracts the key takeaways, drafts a summary, and sends it via email.
This is the email it sent:
It works pretty well and could save significant time for analysts, journalists, or researchers who need to quickly aggregate newsletters from trusted sources in their inbox. You could then schedule it to run daily with Windows Task Scheudler - check out my guide.
One could further refine the prompt to extract more details or gain different insights from the emails. You could also modify the agent to summarize emails from multiple senders and sources each day.
I’ve included the full code for the GMAIL agent, including instructions on setting up the Google API at the end of this article as a bonus for paid subscribers.
Some best practices for using Cursor
Here are a few simple tips for working efficiently with Cursor or any other AI-powered code editor:
Provide detailed context in your prompts:
This is good advice for prompting in general, but the more context you provide—such as example notebooks, sample code, or clearly stated inputs and outputs—the more accurately the code editor can perform the task.
Start simple—test often and break complex tasks down:
Always start with a minimal viable example that works, then gradually add complexity and additional features. Test frequently and iterate.
Keep your code organized and modular:
Clear, modular code makes it easier for the AI to follow your structure and simplifies testing individual components. For example, use a process_data.py
file to handle data processing and a separate visualization.py
file for generating plots.
Use ‘@web’ before your prompt:
This allows the editor to search the web and incorporate recent developments relevant to your coding task.
Choose an appropriate model:
Claude 3.5/3.7 models are great all-rounders for coding and agent mode. However, if you're stuck in a loop or not seeing optimal results, try switching to another model like GPT-4o. Keep in mind that GPT-4o and Claude are premium models, meaning usage may be limited or incur extra costs depending on your subscription plan. For simpler tasks, free models such as DeepSeek V3 can work effectively
Full code for the GMAIL agent
Keep reading with a 7-day free trial
Subscribe to Autonomous Econ to keep reading this post and get 7 days of free access to the full post archives.