Gemini 3.1 Pro API: Hướng dẫn lập trình viên với ví dụ code (2026)
Hướng dẫn đầy đủ cho lập trình viên về Gemini 3.1 Pro API. Bao gồm model ID (gemini-3.1-pro-preview-customtools), giá cả, ví dụ code Python và JavaScript, custom tool, function calling, và cách tích hợp với ứng dụng của bạn.
Tóm tắt
| Gemini 3.1 Pro | |
|---|---|
| Model ID | gemini-3.1-pro, gemini-3.1-pro-preview-customtools |
| Cửa sổ ngữ cảnh | 1M token |
| Giá input | $2/1M token |
| Giá output | $12/1M token |
| Tính năng chính | Custom tool, function calling, grounding, multimodal (text + image + audio + video) |
| API | Google AI Studio / Vertex AI |
Gemini 3.1 Pro là model frontier mới nhất của Google, ra mắt tháng 3/2026. API frontier rẻ nhất theo token, có context native 1M và giới thiệu custom tool — cách mới để cung cấp cho model quyền truy cập vào các hàm bên ngoài với schema có cấu trúc.
Model ID
Google cung cấp hai biến thể của Gemini 3.1 Pro:
| Model ID | Mô tả | Trạng thái |
|---|---|---|
gemini-3.1-pro | Bản phát hành ổn định, khả dụng chung | GA |
gemini-3.1-pro-preview-customtools | Preview với hỗ trợ custom tool nâng cao | Preview |
Biến thể customtools có độ tin cậy cải thiện cho chuỗi function calling phức tạp. Cho sử dụng chung, khuyến nghị dùng gemini-3.1-pro ổn định.
# Google AI Studio
model = "gemini-3.1-pro"
# Vertex AI
model = "gemini-3.1-pro@001"
Bắt đầu nhanh: Python
Cài đặt
pip install google-genai
Tạo text cơ bản
from google import genai
client = genai.Client(api_key="YOUR_API_KEY")
response = client.models.generate_content(
model="gemini-3.1-pro",
contents="Explain quantum computing in 3 sentences."
)
print(response.text)
Streaming
for chunk in client.models.generate_content_stream(
model="gemini-3.1-pro",
contents="Write a Python function to merge two sorted arrays."
):
print(chunk.text, end="")
Bắt đầu nhanh: JavaScript
Cài đặt
npm install @google/genai
Tạo text cơ bản
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
const response = await ai.models.generateContent({
model: "gemini-3.1-pro",
contents: "Explain quantum computing in 3 sentences.",
});
console.log(response.text);
Streaming
const stream = await ai.models.generateContentStream({
model: "gemini-3.1-pro",
contents: "Write a JavaScript function to merge two sorted arrays.",
});
for await (const chunk of stream) {
process.stdout.write(chunk.text);
}
Be first to build with AI
Y Build is the AI-era operating system for startups. Join the waitlist and get early access.
Giá
Gemini 3.1 Pro là API model frontier rẻ nhất tính đến tháng 3/2026.
| Gemini 3.1 Pro | GPT-5.2 | Claude Sonnet 4.6 | |
|---|---|---|---|
| Input | $2/1M | $5/1M | $3/1M |
| Output | $12/1M | $15/1M | $15/1M |
| Context | 1M | 400K | 1M (beta) |
| Chi phí cho 100K input + 20K output | $0.44 | $0.80 | $0.60 |
Ở quy mô lớn, Gemini 3.1 Pro rẻ hơn khoảng 45% so với GPT-5.2 và 27% so với Sonnet 4.6 mỗi phiên.
Gói miễn phí
Google AI Studio cung cấp gói miễn phí:
- 60 request mỗi phút
- 1M token mỗi phút
- Không cần thẻ tín dụng
Đây là gói API miễn phí hào phóng nhất trong ba nhà cung cấp lớn.
Tính năng chính
Cửa sổ ngữ cảnh 1M Token
Gemini 3.1 Pro hỗ trợ native 1 triệu token ngữ cảnh — đủ cho:
- ~700.000 từ text
- ~30.000 dòng code
- ~1 giờ video
- ~11 giờ audio
Custom Tool (Function Calling)
Custom tool cho phép bạn định nghĩa các hàm bên ngoài mà Gemini có thể gọi trong quá trình tạo. Model quyết định khi nào gọi tool, cấu trúc argument, và đưa kết quả vào phản hồi.
Grounding với Google Search
Gemini có thể grounding phản hồi bằng kết quả Google Search real-time.
Multimodal Native
Xử lý text, image, audio và video trong một request duy nhất. Không cần model riêng cho vision hay audio.
Ví dụ code: Custom Tool / Function Calling
Python
from google import genai
from google.genai import types
client = genai.Client(api_key="YOUR_API_KEY")
weather_tool = types.Tool(
function_declarations=[
types.FunctionDeclaration(
name="get_weather",
description="Get the current weather for a city",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"city": types.Schema(
type=types.Type.STRING,
description="City name, e.g. 'San Francisco'"
),
"unit": types.Schema(
type=types.Type.STRING,
enum=["celsius", "fahrenheit"],
description="Temperature unit"
),
},
required=["city"],
),
)
]
)
response = client.models.generate_content(
model="gemini-3.1-pro-preview-customtools",
contents="What's the weather like in Tokyo?",
config=types.GenerateContentConfig(
tools=[weather_tool],
),
)
for part in response.candidates[0].content.parts:
if part.function_call:
print(f"Function: {part.function_call.name}")
print(f"Arguments: {part.function_call.args}")
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
const weatherTool = {
functionDeclarations: [
{
name: "get_weather",
description: "Get the current weather for a city",
parameters: {
type: "OBJECT",
properties: {
city: {
type: "STRING",
description: "City name, e.g. 'San Francisco'",
},
unit: {
type: "STRING",
enum: ["celsius", "fahrenheit"],
description: "Temperature unit",
},
},
required: ["city"],
},
},
],
};
const response = await ai.models.generateContent({
model: "gemini-3.1-pro-preview-customtools",
contents: "What's the weather like in Tokyo?",
config: {
tools: [weatherTool],
},
});
for (const part of response.candidates[0].content.parts) {
if (part.functionCall) {
console.log(`Function: ${part.functionCall.name}`);
console.log(`Arguments:`, part.functionCall.args);
}
}
Ví dụ code: Multimodal (Image + Text)
Python
from google import genai
from google.genai import types
import base64
client = genai.Client(api_key="YOUR_API_KEY")
with open("screenshot.png", "rb") as f:
image_data = f.read()
response = client.models.generate_content(
model="gemini-3.1-pro",
contents=[
types.Content(
parts=[
types.Part(text="What's in this screenshot? Describe the UI elements."),
types.Part(
inline_data=types.Blob(
mime_type="image/png",
data=image_data,
)
),
]
)
],
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
import fs from "fs";
const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
const imageData = fs.readFileSync("screenshot.png");
const base64Image = imageData.toString("base64");
const response = await ai.models.generateContent({
model: "gemini-3.1-pro",
contents: [
{
parts: [
{ text: "What's in this screenshot? Describe the UI elements." },
{
inlineData: {
mimeType: "image/png",
data: base64Image,
},
},
],
},
],
});
console.log(response.text);
So sánh API: Gemini 3.1 Pro vs GPT-5.2 vs Claude Sonnet 4.6
| Tính năng | Gemini 3.1 Pro | GPT-5.2 | Claude Sonnet 4.6 |
|---|---|---|---|
| Giá input | $2/1M | $5/1M | $3/1M |
| Giá output | $12/1M | $15/1M | $15/1M |
| Cửa sổ ngữ cảnh | 1M (GA) | 400K | 1M (beta) |
| Function calling | Có (custom tool) | Có | Có (tool use) |
| Multimodal | Text + image + audio + video | Text + image + audio | Text + image |
| Grounding | Google Search | Web browsing | Không có grounding native |
| Streaming | Có | Có | Có |
| Gói miễn phí | 60 RPM, 1M TPM | Hạn chế | Hạn chế |
| Coding (SWE-bench) | 76.8% | 80.0% | 79.6% |
| Computer use | N/A | 38.2% | 72.5% |
| Toán (AIME) | ~88% | 100% | ~90% |
Khi nào chọn API nào
Chọn Gemini 3.1 Pro: Khi chi phí là ưu tiên, cần xử lý video/audio native, cần 1M context trong production (GA), xây dựng trên Google Cloud. Chọn GPT-5.2: Khi suy luận toán nặng là quan trọng, cần structured output với JSON schema đảm bảo. Chọn Claude Sonnet 4.6: Khi coding và tác vụ agent là use case chính, cần computer use / browser automation.Tích hợp Gemini 3.1 Pro với ứng dụng của bạn
Sử dụng với Y Build
Nếu bạn đang xây sản phẩm với Y Build, bạn có thể tích hợp Gemini API trực tiếp vào backend. Dự án Y Build deploy lên Cloudflare Workers.
// In a Y Build project (Cloudflare Worker)
export async function onRequest(context) {
const response = await fetch(
"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-pro:generateContent",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-goog-api-key": context.env.GEMINI_API_KEY,
},
body: JSON.stringify({
contents: [{ parts: [{ text: "Your prompt here" }] }],
}),
}
);
const data = await response.json();
return new Response(JSON.stringify(data));
}
Rate Limit
| Tầng | Request/phút | Token/phút |
|---|---|---|
| Miễn phí | 60 | 1.000.000 |
| Trả theo sử dụng | 1.000 | 4.000.000 |
| Doanh nghiệp | Tùy chỉnh | Tùy chỉnh |
Câu hỏi thường gặp
gemini-3.1-pro-preview-customtools là gì?
Đó là biến thể preview của Gemini 3.1 Pro được tối ưu cho custom tool và function calling. Cho tạo text chung, hãy dùng gemini-3.1-pro ổn định.
Gemini 3.1 Pro có tốt hơn GPT-5.2 không?
Tùy tác vụ. Gemini rẻ hơn, cửa sổ context lớn hơn và hỗ trợ nhiều modality hơn. GPT-5.2 điểm cao hơn trên benchmark coding và suy luận toán.
Tôi có thể dùng Gemini 3.1 Pro miễn phí không?
Có. Google AI Studio cung cấp gói miễn phí với 60 request mỗi phút và 1 triệu token mỗi phút. Không cần thẻ tín dụng.
Sự khác biệt giữa Google AI Studio và Vertex AI?
Google AI Studio là API đơn giản hơn, hướng lập trình viên. Vertex AI là nền tảng doanh nghiệp trên Google Cloud với fine-tuning, deploy model, monitoring và SLA. Cùng model, khác wrapper.
Kết luận
Gemini 3.1 Pro là API frontier giá trị tốt nhất tháng 3/2026. Với $2/$12 mỗi triệu token, rẻ hơn khoảng một nửa so với GPT-5.2 và rẻ hơn một phần ba so với Claude Sonnet 4.6 — với context native 1M và hỗ trợ multimodal rộng nhất.
Lời khuyên thực tế cho lập trình viên: dùng Gemini cho tác vụ multimodal và nhạy cảm chi phí, Claude cho coding và agent, GPT-5.2 cho suy luận toán nặng.
Đang xây sản phẩm AI? Y Build — AI coding, deploy một click lên Cloudflare, Demo Cut, AI SEO và analytics. Tích hợp Gemini, Claude hoặc GPT API vào ứng dụng và ra mắt trong vài giờ. Bắt đầu miễn phí.
Nguồn:
Be first to build with AI
Y Build is the AI-era operating system for startups. Join the waitlist and get early access.