Firebase Genkit: Complete Guide to Building Production AI Applications
⏱️ Estimated Reading Time: 15 minutes
Introduction
Firebase Genkit is Google’s open-source framework for building AI-powered applications in JavaScript, Go, and Python. Used in production by Google’s Firebase team, Genkit provides a unified interface for integrating AI models from various providers including Google, OpenAI, Anthropic, and Ollama.
In this comprehensive tutorial, we’ll explore how to build, deploy, and monitor production-ready AI applications using Firebase Genkit.
What is Firebase Genkit?
Firebase Genkit is an open-source SDK that simplifies AI application development by providing:
- Unified Model Interface: Work with hundreds of AI models through consistent APIs
- Cross-Language Support: JavaScript/TypeScript (production-ready), Go (production-ready), Python (alpha)
- Production Tools: Built-in monitoring, debugging, and deployment capabilities
- Multimodal Support: Text, image, and structured data generation
- Developer Experience: Local CLI and visual debugging tools
Key Capabilities
Feature | Description |
---|---|
Broad AI Model Support | Unified interface for Google, OpenAI, Anthropic, Ollama models |
Simplified Development | Streamlined APIs for structured output, tool calling, RAG |
Web/Mobile Ready | Seamless integration with Next.js, React, Angular, iOS, Android |
Cross-Language | Consistent APIs across JavaScript, Go, and Python |
Deploy Anywhere | Cloud Functions, Cloud Run, or any hosting platform |
Developer Tools | Local CLI and UI for testing, debugging, evaluation |
Production Monitoring | Comprehensive observability and performance tracking |
Installation and Setup
Prerequisites
- Node.js 18+ (for JavaScript/TypeScript)
- Go 1.21+ (for Go development)
- Python 3.9+ (for Python development)
- API keys for your chosen AI model provider
JavaScript/TypeScript Setup
# Install Genkit CLI globally
npm install -g genkit-cli
# Create new project
mkdir my-genkit-app
cd my-genkit-app
npm init -y
# Install Genkit core and Google AI plugin
npm install genkit @genkit-ai/google-genai
# Install development dependencies
npm install -D typescript @types/node tsx
Go Setup
# Initialize Go module
go mod init my-genkit-app
# Install Genkit for Go
go get github.com/firebase/genkit/go/genkit
go get github.com/firebase/genkit/go/plugins/googleai
Python Setup (Alpha)
# Create virtual environment
python -m venv genkit-env
source genkit-env/bin/activate # On Windows: genkit-env\Scripts\activate
# Install Genkit for Python
pip install genkit google-genai
Basic Usage Examples
JavaScript/TypeScript Example
Create a basic AI application:
// index.ts
import { genkit } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';
// Initialize Genkit with Google AI plugin
const ai = genkit({
plugins: [googleAI()]
});
// Basic text generation
async function generateText() {
const { text } = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: 'Explain quantum computing in simple terms'
});
console.log(text);
}
// Structured output generation
async function generateStructuredData() {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: 'Generate a product review for a smartphone',
output: {
schema: {
type: 'object',
properties: {
product: { type: 'string' },
rating: { type: 'number', minimum: 1, maximum: 5 },
pros: { type: 'array', items: { type: 'string' } },
cons: { type: 'array', items: { type: 'string' } },
summary: { type: 'string' }
},
required: ['product', 'rating', 'summary']
}
}
});
console.log('Structured Review:', response.output);
}
// Run examples
generateText();
generateStructuredData();
Go Example
// main.go
package main
import (
"context"
"fmt"
"log"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/googleai"
)
func main() {
ctx := context.Background()
// Initialize Genkit with Google AI
if err := genkit.Init(ctx, &genkit.Options{
Plugins: []genkit.Plugin{googleai.Plugin()},
}); err != nil {
log.Fatal(err)
}
// Generate text
model := googleai.Model("gemini-2.0-flash-exp")
resp, err := model.Generate(ctx, &genkit.GenerateRequest{
Messages: []*genkit.Message{
{
Content: []*genkit.Part{
genkit.NewTextPart("What are the benefits of cloud computing?"),
},
Role: genkit.RoleUser,
},
},
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Response: %s\n", resp.Candidates[0].Message.Content[0].Text)
}
Python Example (Alpha)
# main.py
import asyncio
from genkit import ai
from genkit.providers.google import google_genai
# Configure Google AI provider
google_genai.configure(api_key="your-api-key")
async def generate_text():
# Basic text generation
response = await ai.generate(
model=google_genai.models.gemini_2_0_flash_exp,
prompt="Explain machine learning to a beginner"
)
print(f"Generated text: {response.text}")
async def main():
await generate_text()
if __name__ == "__main__":
asyncio.run(main())
Advanced Features
Tool Calling and Function Integration
Genkit supports AI agents that can call external tools and functions:
import { defineTool } from 'genkit';
// Define a weather tool
const weatherTool = defineTool({
name: 'getWeather',
description: 'Get current weather for a location',
inputSchema: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' }
},
required: ['location']
},
outputSchema: {
type: 'object',
properties: {
temperature: { type: 'number' },
condition: { type: 'string' },
humidity: { type: 'number' }
}
}
}, async (input) => {
// Simulate weather API call
return {
temperature: 22,
condition: 'Sunny',
humidity: 65
};
});
// Use tool in AI conversation
async function weatherAssistant() {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: 'What\'s the weather like in Seoul?',
tools: [weatherTool]
});
console.log(response.text);
}
Multimodal AI (Text + Images)
Process both text and images in your AI applications:
import { readFileSync } from 'fs';
async function analyzeImage() {
const imageData = readFileSync('path/to/image.jpg');
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: [
{ text: 'Describe what you see in this image' },
{
media: {
contentType: 'image/jpeg',
data: imageData
}
}
]
});
console.log('Image Analysis:', response.text);
}
RAG (Retrieval-Augmented Generation)
Implement context-aware AI with external knowledge:
import { defineRetriever } from 'genkit';
// Define document retriever
const documentRetriever = defineRetriever({
name: 'companyDocs',
configSchema: {
type: 'object',
properties: {
query: { type: 'string' }
}
}
}, async (input) => {
// Simulate document search
return [
{
content: 'Company policy document content...',
metadata: { source: 'employee-handbook.pdf', page: 1 }
}
];
});
// RAG-powered Q&A
async function answerQuestion(question: string) {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: `Answer this question based on the provided context: ${question}`,
config: {
retriever: documentRetriever,
retrieverConfig: { query: question }
}
});
return response.text;
}
Development Workflow
Using the Genkit CLI
The Genkit CLI provides powerful development tools:
# Start development server with UI
genkit start -- npm run dev
# Run specific flow
genkit flow:run myFlow --input '{"query": "test"}'
# Evaluate flows
genkit eval:run --flow myFlow --dataset test-data.json
# Generate traces
genkit trace:list
Developer UI Features
The local Genkit UI provides:
- Flow Playground: Test AI flows with different inputs
- Model Comparison: Compare outputs from different models
- Trace Inspector: Debug execution with detailed traces
- Evaluation Dashboard: Review performance metrics
- Prompt Engineering: Iterate on prompts visually
Access the UI at http://localhost:4000
when running genkit start
.
Production Deployment
Firebase Functions Deployment
// functions/src/index.ts
import { onFlow } from '@genkit-ai/firebase/functions';
import { genkit } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';
const ai = genkit({
plugins: [googleAI()]
});
export const chatFlow = onFlow(ai, {
name: 'chatFlow',
inputSchema: {
type: 'object',
properties: {
message: { type: 'string' }
}
},
outputSchema: {
type: 'object',
properties: {
response: { type: 'string' }
}
}
}, async (input) => {
const { text } = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: input.message
});
return { response: text };
});
Deploy to Firebase:
# Initialize Firebase project
firebase init functions
# Deploy functions
firebase deploy --only functions
Google Cloud Run Deployment
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
EXPOSE 8080
CMD ["npm", "start"]
Deploy to Cloud Run:
# Build and push container
gcloud builds submit --tag gcr.io/PROJECT_ID/genkit-app
# Deploy to Cloud Run
gcloud run deploy genkit-app \
--image gcr.io/PROJECT_ID/genkit-app \
--platform managed \
--region us-central1 \
--allow-unauthenticated
Monitoring and Observability
Setting Up Monitoring
Configure monitoring for production deployments:
import { genkit } from 'genkit';
import { googleCloudTrace } from '@genkit-ai/google-cloud';
const ai = genkit({
plugins: [
googleAI(),
googleCloudTrace({
projectId: 'your-project-id'
})
],
telemetry: {
instrumentation: 'googleCloud',
logger: 'googleCloud'
}
});
Monitoring Dashboard
The Firebase console provides:
- Request Volume: Track API call frequency
- Latency Metrics: Monitor response times
- Error Rates: Identify and debug failures
- Cost Analysis: Monitor token usage and costs
- Model Performance: Compare model effectiveness
Best Practices
1. Error Handling
import { GenkitError } from 'genkit';
async function robustGeneration(prompt: string) {
try {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt,
config: {
maxRetries: 3,
timeout: 30000
}
});
return response.text;
} catch (error) {
if (error instanceof GenkitError) {
console.error('Genkit Error:', error.message);
// Handle specific error types
switch (error.code) {
case 'RATE_LIMIT_EXCEEDED':
// Implement backoff strategy
break;
case 'INVALID_REQUEST':
// Handle invalid input
break;
}
}
throw error;
}
}
2. Input Validation
import Joi from 'joi';
const inputSchema = Joi.object({
query: Joi.string().min(1).max(1000).required(),
language: Joi.string().valid('en', 'ko', 'ar').default('en')
});
async function validateAndProcess(input: any) {
const { error, value } = inputSchema.validate(input);
if (error) {
throw new Error(`Invalid input: ${error.message}`);
}
return value;
}
3. Prompt Engineering
const SYSTEM_PROMPT = `
You are a helpful AI assistant specialized in technical documentation.
Always provide accurate, well-structured responses with examples when appropriate.
If you're unsure about something, clearly state your uncertainty.
`;
async function generateDocumentation(topic: string) {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: `${SYSTEM_PROMPT}\n\nTopic: ${topic}\n\nProvide comprehensive documentation:`,
config: {
temperature: 0.3, // Lower temperature for technical content
maxOutputTokens: 2000
}
});
return response.text;
}
4. Cost Optimization
// Use appropriate models for different tasks
const MODEL_CONFIG = {
simple: googleAI.model('gemini-2.0-flash-exp'), // Fast, cost-effective
complex: googleAI.model('gemini-2.0-pro'), // More capable, higher cost
multimodal: googleAI.model('gemini-2.0-pro-vision') // Image + text
};
function selectModel(taskComplexity: 'simple' | 'complex' | 'multimodal') {
return MODEL_CONFIG[taskComplexity];
}
Troubleshooting Common Issues
Authentication Problems
# Set Google Cloud credentials
export GOOGLE_APPLICATION_CREDENTIALS="path/to/service-account-key.json"
# Or use gcloud CLI
gcloud auth application-default login
Model Access Issues
// Check model availability
const availableModels = await googleAI.listModels();
console.log('Available models:', availableModels);
// Handle model-specific errors
try {
const response = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: 'test'
});
} catch (error) {
if (error.message.includes('model not found')) {
console.log('Try using gemini-1.5-pro instead');
}
}
Performance Optimization
// Implement request caching
const cache = new Map();
async function cachedGeneration(prompt: string) {
const cacheKey = `generation:${prompt}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
const result = await ai.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt
});
cache.set(cacheKey, result.text);
return result.text;
}
Testing Strategies
Unit Testing
import { describe, it, expect, beforeEach } from 'vitest';
describe('AI Functions', () => {
beforeEach(() => {
// Setup test environment
});
it('should generate valid responses', async () => {
const response = await generateText('test prompt');
expect(response).toBeDefined();
expect(typeof response).toBe('string');
expect(response.length).toBeGreaterThan(0);
});
it('should handle structured output', async () => {
const result = await generateStructuredData();
expect(result).toHaveProperty('product');
expect(result.rating).toBeGreaterThanOrEqual(1);
expect(result.rating).toBeLessThanOrEqual(5);
});
});
Integration Testing
// test/integration.test.ts
import { genkit } from 'genkit';
import { googleAI } from '@genkit-ai/google-genai';
const testAI = genkit({
plugins: [googleAI()],
environment: 'test'
});
describe('Integration Tests', () => {
it('should integrate with Google AI successfully', async () => {
const response = await testAI.generate({
model: googleAI.model('gemini-2.0-flash-exp'),
prompt: 'Say hello'
});
expect(response.text).toContain('hello');
});
});
Security Considerations
API Key Management
// Use environment variables for API keys
const config = {
googleAI: {
apiKey: process.env.GOOGLE_AI_API_KEY || '',
projectId: process.env.GOOGLE_CLOUD_PROJECT || ''
}
};
// Validate configuration
if (!config.googleAI.apiKey) {
throw new Error('GOOGLE_AI_API_KEY environment variable is required');
}
Input Sanitization
import DOMPurify from 'isomorphic-dompurify';
function sanitizeInput(input: string): string {
// Remove potentially harmful content
const cleaned = DOMPurify.sanitize(input);
// Additional validation
if (cleaned.length > 10000) {
throw new Error('Input too long');
}
return cleaned;
}
Rate Limiting
import rateLimit from 'express-rate-limit';
const aiRateLimit = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
message: 'Too many AI requests from this IP'
});
// Apply to AI endpoints
app.use('/api/ai', aiRateLimit);
Conclusion
Firebase Genkit provides a powerful, production-ready framework for building AI applications. Its unified interface, cross-language support, and comprehensive tooling make it an excellent choice for developers looking to integrate AI capabilities into their applications.
Key takeaways:
- Start Simple: Begin with basic text generation and gradually add complex features
- Use the Tools: Leverage the CLI and Developer UI for efficient development
- Plan for Production: Implement proper monitoring, error handling, and security
- Optimize Costs: Choose appropriate models and implement caching strategies
- Test Thoroughly: Use comprehensive testing strategies for reliable AI applications
With Firebase Genkit, you can build sophisticated AI applications that scale from prototype to production with confidence.
Next Steps
- Explore the official Genkit documentation
- Try the Genkit samples
- Join the Genkit Discord community
- Experiment with different model providers and capabilities
- Build your own AI-powered application using the concepts covered in this tutorial