As a powerful ChatGPT alternative, Gemini offers multimodal model support for video processing and more. When integrating the Gemini API into production, we encountered a typical problem: the 1 million token context window handled large documents, but p99 latency reached 12 seconds. The reason was the lack of chunking and incorrect generation_config settings. Our client from fintech wanted to process quarterly reports (up to 500 pages) in real time. Without optimization, the model produced responses in 10–15 seconds, which was unacceptable for a chat.
A common mistake when integrating is using the model without chunking on long documents: tokens fill the context, making the response slow or incomplete. We developed a chunking strategy with a 200-token overlap, reducing p99 to 2 seconds on documents up to 1 million tokens. Additionally, incorrect safety settings can block legitimate requests — we configure thresholds for each scenario to avoid losing important data. Unlike GPT-4o, Gemini provides more context (1M vs 128K) but requires precise tuning of generation_config. For analytical tasks, we set temperature 0.2; for creative, 0.9. Gemini also natively handles video and audio, opening scenarios for media platforms.
Solving Problems with Gemini API Integration
High latency on long contexts
Without chunking and streaming, response time grows linearly. We split documents into 2000-token chunks with a 200-token overlap. Streaming with stream=True reduces perceived latency by 3x.
Unstable JSON response
The model sometimes returns invalid JSON if response_mime_type is not specified. We force application/json and add a fallback check.
Noisy results from multimodal requests
Low-quality images or video with artifacts. We normalize input: compress images to 1024×1024, convert video to H.264.
How we do it: a case with RAG and Function Calling
In our practice, there was a project for a fintech startup: an assistant analyzes company reports and answers questions in real time. Stack: Gemini 1.5 Pro, pgvector for embeddings (1536-dim), LangChain for orchestration. To get accurate numeric answers, we added Function Calling — the model calls get_financial_metric(ticker, period) and returns data from the database.
import google.generativeai as genai
def get_financial_metric(ticker: str, period: str) -> dict:
# query SQL database
return {"ticker": ticker, "revenue": 125000000, "currency": "EUR"}
model = genai.GenerativeModel("gemini-1.5-pro", tools=[get_financial_metric])
response = model.generate_content("Apple (AAPL) profit for the last reported quarter")
print(response.text) # (metric value)
Without Function Calling, the model could hallucinate numbers. With the tool, accuracy is guaranteed.
Work process
| Stage | What we do | Details | Result |
|---|---|---|---|
| Analysis | Study use cases, peak loads | Analyze up to 10 use cases, identify peak loads, select model (Pro/Flash), token budget | Technical specification, model selection, token budget |
| Design | Design architecture: chunking, caching, security | Design chunking with 200-token overlap, embedding caching, IAM roles, safety playbook | Flow diagram, safety settings playbook |
| Implementation | Write integration code, configure streaming, Function Calling | Write code with streaming (max_output_tokens=1024), implement function calling, add fallback for network errors | Repository with documentation, latency tests |
| Testing | Load test with synthetic data, check p99 | Run load tests with 50 parallel requests, measure p99 latency, optimize connection pool | Load report, optimization recommendations |
| Deployment | Deploy on Vertex AI or your Kubernetes | Configure autoscaling, monitoring (logs, metrics), set up CI/CD pipeline | Endpoint access, monitoring dashboards |
Estimated timelines
- Basic integration of one endpoint — from 1 day.
- Scenario with RAG and Function Calling — from 3 days.
- Full production with Vertex AI and CI/CD — up to a week.
Cost is calculated individually — contact us, we'll evaluate your project within 24 hours. Basic integration starts at $500, while advanced scenarios with RAG and function calling begin at $1,500. Typical savings: by using Gemini Flash for simple queries, you can cut costs by up to 40% compared to Pro, often saving $500–$2,000 per month depending on volume.
What's included
- Integration code with Gemini API (Python / TypeScript).
- Documentation for endpoints and content generation.
- Access and IAM configuration (Google Cloud Service Account).
- Team training (2 hours online).
- Post-deployment support — 2 weeks.
Model comparison: GPT-4o vs Gemini Pro vs Flash
| Characteristic | Gemini 1.5 Pro | Gemini 1.5 Flash | GPT-4o |
|---|---|---|---|
| Context window | 1M tokens | 1M tokens | 128K tokens |
| Speed (p50) | ~2–5 s | ~0.5–1 s | ~1–3 s |
| Multimodality | text, images, audio, video | text, images | text, images |
Gemini Flash is 5x faster than Pro in p50 latency. Flash is the number one choice for chats where speed matters. Pro is for deep document analysis. Flash is more cost-efficient than Pro at high volumes.
Optimizing p99 latency
We use aiohttp connection pools, enable streaming, and set max_output_tokens = 1024 for short responses. For Flash model, p99 latency stays under 1 second with 50 parallel requests. Streaming setup:
- Install
google-generativeai>=0.3.0. - Initialize the model:
genai.GenerativeModel("gemini-1.5-flash"). - Set
generation_configwithmax_output_tokens=1024andtemperature=0.2. - Call
model.generate_content(..., stream=True). - Process chunks in a loop
for chunk in response:.
Common integration mistakes: safety settings not configured (model blocks valid requests), no fallback (client crashes on network error), ignoring rate limits (free tier has 60 RPM, in production switch to paid tier or Vertex AI).
Why Vertex AI for enterprise?
Vertex AI provides IAM, VPC-SC, auditing, and SLA 99.9%, along with MLOps features like model registry and monitoring. Our team has extensive experience in ML integration across various industries. We have Google Cloud certifications and experience deploying in europe-west4 and us-central1 regions. With extensive experience in AI development, we have implemented over 50 integrations with Google AI, including major fintech and e-commerce projects.
Reducing API costs
Proper model selection and configuration reduce costs. For simple requests, use Flash; for complex, Pro. Caching embeddings reduces the number of API calls. For more on cost reduction, see Google AI Documentation at https://developers.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling. Contact us for a consultation on your project and a budget estimate within 24 hours. Order integration today and get a free audit of your current AI pipeline.







