Gemini Flex API เป็นระดับการอนุมานที่ช่วยลดต้นทุนได้ 50% เมื่อเทียบกับอัตรามาตรฐาน โดยแลกกับการตอบสนองที่ผันแปรและความพร้อมใช้งาน ตามความพยายามอย่างเต็มที่ ออกแบบมาสำหรับภาระงานที่ยอมรับเวลาในการตอบสนองได้ซึ่งต้องมีการประมวลผลแบบ ซิงโครนัส แต่ไม่จำเป็นต้องมีประสิทธิภาพแบบเรียลไทม์ของ API มาตรฐาน
วิธีใช้ Flex
หากต้องการใช้ระดับ Flex ให้ระบุ service_tier เป็น flex ในคำขอ โดยค่าเริ่มต้น คำขอจะใช้ระดับมาตรฐานหากละเว้นช่องนี้
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="Analyze this dataset for trends...",
service_tier='flex'
)
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from '@google/genai';
const client = new GoogleGenAI({});
async function main() {
const interaction = await client.interactions.create({
model: 'gemini-3.8-flash',
input: 'Analyze this dataset for trends...',
service_tier: 'flex'
});
console.log(interaction.output_text);
}
await main();
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ServiceTier;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
Client client = new Client();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("Analyze this dataset for trends..."))
.serviceTier(ServiceTier.FLEX)
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
System.out.println(interaction.outputText().orElse(""));
Go
package main
import (
"context"
"fmt"
"log"
"google.golang.org/genai"
"google.golang.org/genai/interactions/models/interactions"
"google.golang.org/genai/interactions/models/operations"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("Analyze this dataset for trends..."),
ServiceTier: interactions.ServiceTierFlex.ToPointer(),
}),
})
if err != nil {
log.Fatal(err)
}
if res.Interaction.OutputText != nil {
fmt.Println(*res.Interaction.OutputText)
}
}
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"model": "gemini-3.8-flash",
"input": "Analyze this dataset for trends...",
"service_tier": "flex"
}'
วิธีการทำงานของ Flex Inference
Gemini Flex Inference ช่วยลดช่องว่างระหว่าง API มาตรฐานกับเวลาในการตอบกลับ 24 ชั่วโมงของ Batch API โดยจะใช้ความสามารถในการประมวลผลในช่วงนอกเวลาเร่งด่วนที่ "ลดได้" เพื่อมอบโซลูชันที่คุ้มค่าสำหรับงานที่ทำงานเบื้องหลังและเวิร์กโฟลว์แบบลำดับ
| ฟีเจอร์ | พับ | ลำดับความสำคัญ | มาตรฐาน | กลุ่ม |
|---|---|---|---|---|
| การกำหนดราคา | ส่วนลด 50% | มากกว่ารุ่น Standard 75-100% | ตั๋วราคาเต็ม | ส่วนลด 50% |
| เวลาในการตอบสนอง | นาที (เป้าหมาย 1-15 นาที) | ต่ำ (วินาที) | วินาทีถึงนาที | สูงสุด 24 ชั่วโมง |
| ความน่าเชื่อถือ | อย่างเต็มที่ (ลดภาระได้) | สูง (ไม่หลุดร่วง) | สูง / สูงปานกลาง | สูง (สำหรับปริมาณงาน) |
| อินเทอร์เฟซ | ซิงโครนัส | ซิงโครนัส | ซิงโครนัส | อะซิงโครนัส |
ประโยชน์สำคัญ
- ความคุ้มค่า: ประหยัดค่าใช้จ่ายได้อย่างมากสำหรับการประเมินที่ไม่ใช่การผลิต เอเจนต์พื้นหลัง และการเพิ่มคุณค่าของข้อมูล
- ใช้งานง่าย: เพียงเพิ่มพารามิเตอร์เดียวลงในคำขอที่มีอยู่
- เวิร์กโฟลว์แบบซิงโครนัส: เหมาะสำหรับเชน API แบบลำดับที่คำขอถัดไปขึ้นอยู่กับเอาต์พุตของคำขอก่อนหน้า ทำให้มีความยืดหยุ่นมากกว่าการประมวลผลแบบกลุ่มสำหรับเวิร์กโฟลว์แบบ Agent
กรณีการใช้งาน
- การประเมินแบบออฟไลน์: การเรียกใช้การทดสอบการถดถอยหรือลีดเดอร์บอร์ด "LLM-as-a-judge"
- เอเจนต์เบื้องหลัง: งานตามลำดับ เช่น การอัปเดต CRM การสร้างโปรไฟล์ หรือการกลั่นกรองเนื้อหาที่ยอมรับความล่าช้าได้
- การวิจัยที่ถูกจำกัดด้วยงบประมาณ: การทดลองทางวิชาการที่ต้องใช้โทเค็นจำนวนมากภายใต้งบประมาณที่จำกัด
ขีดจำกัดอัตรา
การเข้าชม Flex Inference จะนับรวมในขีดจำกัดอัตราทั่วไปของคุณ โดยจะไม่มีขีดจำกัดอัตราเพิ่มเติมเหมือนกับ Batch API
ความจุที่ลดได้
ระบบจะถือว่าการรับส่งข้อมูลแบบยืดหยุ่นมีความสำคัญต่ำกว่า หากมีการเข้าชมมาตรฐานเพิ่มขึ้นอย่างรวดเร็ว ระบบอาจขัดจังหวะหรือนำคำขอแบบยืดหยุ่นออกเพื่อให้มั่นใจว่ามีทรัพยากรเพียงพอสำหรับผู้ใช้ที่มีลำดับความสำคัญสูง หากคุณกำลังมองหาการอนุมานที่มีลำดับความสำคัญสูง ให้ดูที่การอนุมานตามลำดับความสำคัญ
รหัสข้อผิดพลาด
เมื่อความจุแบบยืดหยุ่นไม่พร้อมใช้งานหรือระบบมีปริมาณการใช้งานสูง API จะ แสดงรหัสข้อผิดพลาดมาตรฐาน
- 503 ไม่พร้อมให้บริการ: ขณะนี้ระบบมีผู้ใช้เต็มแล้ว
- 429 คำขอมากเกินไป: ขีดจำกัดอัตราหรือทรัพยากรหมด
ความรับผิดชอบของลูกค้า
- ไม่มีการสำรองข้อมูลฝั่งเซิร์ฟเวอร์: เพื่อป้องกันการเรียกเก็บเงินที่ไม่คาดคิด ระบบจะไม่ อัปเกรดคำขอ Flex เป็นระดับมาตรฐานโดยอัตโนมัติหากความจุของ Flex เต็ม
- การลองใหม่: คุณต้องใช้ตรรกะการลองใหม่ฝั่งไคลเอ็นต์ของคุณเองด้วย Exponential Backoff
- การหมดเวลา: เนื่องจากคำขอ Flex อาจอยู่ในคิว เราจึงขอแนะนำให้เพิ่ม การหมดเวลาฝั่งไคลเอ็นต์เป็น 10 นาทีขึ้นไปเพื่อหลีกเลี่ยงการปิด การเชื่อมต่อก่อนเวลา
ปรับกรอบเวลาหมดเวลา
คุณสามารถกำหนดค่าการหมดเวลาต่อคำขอสำหรับ REST API และไลบรารีของไคลเอ็นต์ได้ ตรวจสอบเสมอว่าการหมดเวลาฝั่งไคลเอ็นต์ครอบคลุมระยะเวลาที่เซิร์ฟเวอร์ตั้งใจรอ (เช่น 600 วินาทีขึ้นไปสำหรับคิวรอแบบยืดหยุ่น) SDK คาดหวังค่าการหมดเวลาเป็นมิลลิวินาที
การหมดเวลาต่อคำขอ
Python
from google import genai
client = genai.Client(http_options={"timeout": 900000})
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="why is the sky blue?",
service_tier="flex",
)
JavaScript
import { GoogleGenAI } from '@google/genai';
const client = new GoogleGenAI({});
async function main() {
const interaction = await client.interactions.create({
model: "gemini-3.8-flash",
input: "why is the sky blue?",
service_tier: "flex",
}, {timeout: 900000});
}
await main();
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ServiceTier;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
import com.google.genai.types.HttpOptions;
Client client =
Client.builder()
.httpOptions(HttpOptions.builder().timeout(900000).build())
.build();
CreateModelInteraction params =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("why is the sky blue?"))
.serviceTier(ServiceTier.FLEX)
.build();
Interaction interaction =
client.interactions.create(CreateInteractionRequestBody.of(params)).interaction().get();
Go
package main
import (
"context"
"log"
"time"
"google.golang.org/genai"
"google.golang.org/genai/interactions/models/interactions"
"google.golang.org/genai/interactions/models/operations"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{
Timeout: genai.Ptr(15 * time.Minute),
},
})
if err != nil {
log.Fatal(err)
}
res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("why is the sky blue?"),
ServiceTier: interactions.ServiceTierFlex.ToPointer(),
}),
})
if err != nil {
log.Fatal(err)
}
_ = res
}
ใช้การลองใหม่
เนื่องจาก Flex สามารถหยุดทำงานและล้มเหลวด้วยข้อผิดพลาด 503 ได้ นี่คือตัวอย่างการ ใช้ตรรกะการลองใหม่โดยไม่บังคับเพื่อดำเนินการต่อกับคำขอที่ไม่สำเร็จ
Python
import time
from google import genai
client = genai.Client()
def call_with_retry(max_retries=3, base_delay=5):
for attempt in range(max_retries):
try:
return client.interactions.create(
model="gemini-3.8-flash",
input="Analyze this batch statement.",
service_tier="flex",
)
except Exception as e:
if attempt < max_retries - 1:
delay = base_delay * (2 ** attempt) # Exponential Backoff
print(f"Flex busy, retrying in {delay}s...")
time.sleep(delay)
else:
print("Flex exhausted, falling back to Standard...")
return client.interactions.create(
model="gemini-3.8-flash",
input="Analyze this batch statement."
)
interaction = call_with_retry()
print(interaction.output_text)
JavaScript
import { GoogleGenAI } from '@google/genai';
const ai = new GoogleGenAI({});
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function callWithRetry(maxRetries = 3, baseDelay = 5) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
console.log(`Attempt ${attempt + 1}: Calling Flex tier...`);
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: "Analyze this batch statement.",
service_tier: 'flex',
});
return interaction;
} catch (e) {
if (attempt < maxRetries - 1) {
const delay = baseDelay * (2 ** attempt);
console.log(`Flex busy, retrying in ${delay}s...`);
await sleep(delay * 1000);
} else {
console.log("Flex exhausted, falling back to Standard...");
return await ai.interactions.create({
model: "gemini-3.8-flash",
input: "Analyze this batch statement.",
});
}
}
}
}
async function main() {
const interaction = await callWithRetry();
console.log(interaction.output_text);
}
await main();
Java
import com.google.genai.Client;
import com.google.genai.gaos.models.interactions.CreateModelInteraction;
import com.google.genai.gaos.models.interactions.Interaction;
import com.google.genai.gaos.models.interactions.InteractionsInput;
import com.google.genai.gaos.models.interactions.Model;
import com.google.genai.gaos.models.interactions.ServiceTier;
import com.google.genai.gaos.models.operations.CreateInteractionRequestBody;
Client client = new Client();
int maxRetries = 3;
int baseDelay = 5;
Interaction interaction = null;
for (int attempt = 0; attempt < maxRetries; attempt++) {
try {
CreateModelInteraction flexParams =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("Analyze this batch statement."))
.serviceTier(ServiceTier.FLEX)
.build();
interaction =
client.interactions.create(CreateInteractionRequestBody.of(flexParams)).interaction().get();
break;
} catch (Exception e) {
if (attempt < maxRetries - 1) {
int delay = baseDelay * (1 << attempt); // Exponential Backoff
System.out.println("Flex busy, retrying in " + delay + "s...");
Thread.sleep(delay * 1000L);
} else {
System.out.println("Flex exhausted, falling back to Standard...");
CreateModelInteraction standardParams =
CreateModelInteraction.builder()
.model(Model.of("gemini-3.8-flash"))
.input(InteractionsInput.of("Analyze this batch statement."))
.build();
interaction =
client
.interactions
.create(CreateInteractionRequestBody.of(standardParams))
.interaction()
.get();
}
}
}
if (interaction != null) {
System.out.println(interaction.outputText().orElse(""));
}
Go
package main
import (
"context"
"fmt"
"log"
"time"
"google.golang.org/genai"
"google.golang.org/genai/interactions/models/interactions"
"google.golang.org/genai/interactions/models/operations"
)
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, nil)
if err != nil {
log.Fatal(err)
}
maxRetries := 3
baseDelay := 5
var interaction *interactions.Interaction
for attempt := 0; attempt < maxRetries; attempt++ {
res, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("Analyze this batch statement."),
ServiceTier: interactions.ServiceTierFlex.ToPointer(),
}),
})
if err == nil {
interaction = res.Interaction
break
}
if attempt < maxRetries-1 {
delay := baseDelay * (1 << attempt) // Exponential Backoff
fmt.Printf("Flex busy, retrying in %ds...\n", delay)
time.Sleep(time.Duration(delay) * time.Second)
} else {
fmt.Println("Flex exhausted, falling back to Standard...")
stdRes, err := client.Interactions.Create(ctx, operations.CreateInteractionRequest{
Body: operations.NewCreateInteractionRequestBody(interactions.CreateModelInteraction{
Model: interactions.Model("gemini-3.8-flash"),
Input: interactions.NewInteractionsInput("Analyze this batch statement."),
}),
})
if err != nil {
log.Fatal(err)
}
interaction = stdRes.Interaction
}
}
if interaction != nil && interaction.OutputText != nil {
fmt.Println(*interaction.OutputText)
}
}
ราคา
Flex Inference มีราคาอยู่ที่ 50% ของ API มาตรฐาน และเรียกเก็บเงินต่อโทเค็น
รุ่นที่รองรับ
โมเดลต่อไปนี้รองรับการอนุมานแบบยืดหยุ่น
| รุ่น | Flex Inference |
|---|---|
| Gemini 3.8 Flash | ✔️ |
| Gemini 3.7 Flash | ✔️ |
| Gemini 3.6 Flash | ✔️ |
| Gemini 3.5 Flash-Lite | ✔️ |
| Gemini 3.5 Flash | ✔️ |
| Gemini 3.1 Flash-Lite | ✔️ |
| Gemini 3.1 Pro เวอร์ชันตัวอย่าง | ✔️ |
| ตัวอย่าง Gemini 3 Flash | ✔️ |
| Gemini 2.5 Pro | ✔️ |
| Gemini 2.5 Flash | ✔️ |
| Gemini 2.5 Flash-Lite | ✔️ |
ขั้นตอนถัดไป
- การอนุมานลำดับความสำคัญสำหรับเวลาในการตอบสนองต่ำมาก
- โทเค็น: ทำความเข้าใจโทเค็น