SPB Git

Repository Public

optimization.md 5.2 KB 94 lines markdown

1The Gemini API offers a variety of optimization mechanisms to help you balance2speed, cost, and reliability based on your specific workload needs.3Whether you're building real-time conversational bots or running heavy offline4data-processing pipelines, choosing the right paradigm can significantly cut5costs or boost performance.67| Feature | Standard | Flex | Priority | Batch | Caching |8|---|---|---|---|---|---|9| **Pricing** | Full Price | 50% discount | 75% to 100% more than standard | 50% discount | 90% discount + Prorated token storage |10| **Latency** | Seconds to minutes | Minutes (1--15 min target) | Seconds | Up to 24 hours | Faster time-to-first-token |11| **Reliability** | High / Medium-high | Best-effort (Sheddable) | High (Non-sheddable) | High (for throughput) | N/A |12| **Interface** | Synchronous | Synchronous | Synchronous | Asynchronous | Saved state |13| **Best use case** | General application workflows | Non-urgent sequential chains | Production, user-facing apps | Massive datasets, offline evals | Recurring queries over same file |1415## Inference service tiers (Synchronous)1617You can shift between reliability-optimized and cost-optimized synchronous traffic18by passing the `service_tier` parameter in your standard generation calls.1920### Standard inference (Default)2122The standard tier is the default option for sequential content generation.23It provides normal response times without extra premiums or heavy queuing.2425- **Reliability:** Standard criticality26- **Price:** Standard pricing.27- **Best For:** Most interactive day-to-day applications.2829### Priority inference (Latency-optimized)3031[Priority](https://ai.google.dev/gemini-api/docs/priority-inference) processing routes your requests32to high-criticality compute queues.33This traffic is strictly non-sheddable (never preempted by other tiers) and34offers the highest reliability. If you exceed dynamic Priority limits,35the system will gracefully downgrade the request to Standard processing instead36of failing with an error.3738- **Reliability:** Highest criticality39- **Price:** 75% to 100% over Standard rates.40- **Best for:** Customer chatbots, real-time fraud detection, and business-critical copilots.4142### Flex inference (Cost-optimized)4344[Flex inference](https://ai.google.dev/gemini-api/docs/flex-inference) offers a 50% discount45compared to standard rates by utilizing46opportunistic, off-peak compute capacity. Requests are processed47synchronously, meaning you don't need to rewrite code to manage batch objects.48Because it is "sheddable" traffic, requests may be preempted if the system49experiences standard traffic spikes.5051- **Reliability:** Non-guaranteed, sheddable criticality52- **Price:** 50% of Standard Pricing (billed per token).53- **Best for:** Multi-step agentic workflows where call N+1 depends on the output of call N, background CRM updates, and offline evaluations.5455## Batch API (Bulk, asynchronous)5657[The Batch API](https://ai.google.dev/gemini-api/docs/batch-api) is designed to process large volumes58of requests asynchronously at5950% of the standard cost. You can submit requests either as in-line dictionaries60or using a JSONL input file (up to 2GB). It processes requests using background61throughput queues with a target turnaround time of 24 hours.6263- **Reliability:** Sheddable but with 24h automated retries and queuing system64- **Price:** 50% of Standard pricing.65- **Best for:** Pre-processing massive datasets, running periodic regression test suites, and high-volume image or embedding generations.6667## Context caching (Input savings)6869[Context caching](https://ai.google.dev/gemini-api/docs/caching) is used when a substantial initial70context is referenced repeatedly by shorter requests.7172- **Implicit caching:** Automatically enabled on Gemini 2.5 and newer models. The system passes on cost savings if your request hits existing caches based on common prompt prefixes.73- **Explicit Caching:** You can manually create a cache object with a specific Time-To-Live (TTL). Once created, you refer to the cached tokens for subsequent requests to avoid passing the same corpus payload repeatedly.74- **Price:** Billed based on cache token count and storage duration (TTL).75- **Best For:** Chatbots with extensive system instructions, repetitive analysis of lengthy video files, or queries against large document sets.7677## Reduce video input costs with agentic processing7879For long-form video content, agentic processing can reduce input token costs by80up to 88% while improving response quality. Instead of extracting every frame81at 1 FPS (static processing), the model dynamically seeks through the video,82loading transcript and/or frames and/or audio relevant to your prompt. For83short clips (under 5 minutes) where latency is critical, static processing84may provide a faster Time to First Token (TTFT), as agentic mode incurs85internal reasoning and tool round-trips before generation begins.8687To use agentic processing with Gemini 3.7 Flash, 3.6 Flash, or 3.5 Flash Lite,88set `processing: "agentic"` (Interactions API) or89`media_processing: "AGENTIC"` (GenerateContent API) on your video input. If the90model does not support agentic processing, it will return an error.9192See93[Agentic video understanding](https://ai.google.dev/gemini-api/docs/video-understanding#agentic-video-understanding)94for code examples.
Files main
Menu

Theme