Advanced
⏱ 40 min read
© Gate of AI 2026
Learn how to build a dual-engine AI agent in Python that intelligently routes tasks between GPT-4o and Gemini 2.5 using modern SDKs, with built-in memory and smart model selection.
Prerequisites
- Python 3.10 or higher
- OpenAI API key and Google Gemini API key
- Basic understanding of Python and API usage
What You’ll Learn
- How to initialize modern OpenAI and Google GenAI clients
- Intelligent task routing between GPT-4o and Gemini
- Implementing lightweight memory for context retention
- Secure API key management with environment variables
What We’re Building
In this tutorial, we will build a dual-engine AI agent that can dynamically choose between GPT-4o and Gemini 2.5 Flash depending on the task.
The agent uses a simple but effective routing system to decide which model to use, while also maintaining memory of previous interactions. This pattern helps balance cost, speed, and output quality.
Setup and Installation
Install the required packages:
pip install openai google-genai python-dotenvCreate a .env file to store your API keys:
# .env
OPENAI_API_KEY=sk-...
GEMINI_API_KEY=AIza...Step 1: Initializing the Dual-Engine Agent
We begin by setting up the agent class with both OpenAI and Google GenAI clients using the latest SDK patterns.
import os
from dotenv import load_dotenv
from openai import OpenAI
from google import genai
load_dotenv()
class DualEngineAgent:
def init(self):
self.memory = []
self.openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
self.gemini_client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
def query_openai(self, prompt: str) -> str:
response = self.openai_client.chat.completions.create(
...Continue Reading
Log in for free to read the rest of this article and access exclusive AI tools.
Log in / Register