An introduction to building type-safe, modular AI agents with TypeScript and Mastra.

We hear the term "Agentic" everywhere. Some are sophisticated; most are just simple wrappers. I’ve spent the last few months building Gemoniq, and I’ve learned that the secret isn't just a better prompt, but also a better architecture.
Today, I want to show you how to set up a basic AI agent in TypeScript using Mastra. It’s the library we use to power Gemoniq because it’s type-safe and incredibly straightforward.
Refer this manual install doc https://mastra.ai/docs/getting-started/manual-install, till step 3 to manually install the required packages. This would prevent unnecessary bloat for now.

Let’s create a new agent. We’ll name it "Leo."
mkdir -p src/mastra/agents && touch src/mastra/agents/leo.ts
Don't forget to add the API Key for your model in .env.
ANTHROPIC_API_KEY=<your-api-key>
GOOGLE_GENERATIVE_AI_API_KEY=<your-api-key>
OPENAI_API_KEY=<your-api-key>Now lets setup the index file in mastra folder, the central place to export our agents.
touch src/mastra/index.ts
Lets create a new file to run this agent.
touch agentRunner.ts
Ensure that we include ".ts" in the include field in tsconfig.json, to compile it before running the file.
Finally, run the following command in the terminal:
npx tsx --env-file=.env agentRunner.tsAnd that is it, you created a very basic agent. Next steps would be to add memory to the agent, then some tools, workflows and skills. I'll be writing about that as well very soon.
🔗 Github link: https://github.com/yashsharmafastlane/agent-intro
We launched our project this week! 🚀 If you want to see how such agents are performing in production, check us out here:
https://peerlist.io/fastlanedevs/project/gemoniq--aipowered-marketing-agency
Goes without saying, any support or feedback is highly appreciated.
0
0
0