Gemini 3.1 Pro API: Guida per Sviluppatori con Esempi di Codice (2026)
Guida completa per sviluppatori all'API Gemini 3.1 Pro. Copre model ID (gemini-3.1-pro-preview-customtools), prezzi, esempi di codice in Python e JavaScript, custom tools, function calling e integrazione nella tua app.
Riepilogo
| Gemini 3.1 Pro | |
|---|---|
| Model IDs | gemini-3.1-pro, gemini-3.1-pro-preview-customtools |
| Finestra di contesto | 1M token |
| Prezzo input | $2/1M token |
| Prezzo output | $12/1M token |
| Funzionalità chiave | Custom tools, function calling, grounding, multimodale (testo + immagine + audio + video) |
| API | Google AI Studio / Vertex AI |
Gemini 3.1 Pro è l'ultimo modello frontier di Google, rilasciato a marzo 2026. È l'API frontier più economica per token, con contesto nativo di 1M, e introduce i custom tools.
Model IDs
| Model ID | Descrizione | Stato |
|---|---|---|
gemini-3.1-pro | Release stabile, disponibilità generale | GA |
gemini-3.1-pro-preview-customtools | Preview con supporto migliorato per custom tools | Preview |
# Google AI Studio
model = "gemini-3.1-pro"
# Vertex AI
model = "gemini-3.1-pro@001"
Quick Start: Python
pip install google-genai
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)
Quick Start: JavaScript
npm install @google/genai
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);
Be first to build with AI
Y Build is the AI-era operating system for startups. Join the waitlist and get early access.
Prezzi
| 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 |
| Contesto | 1M | 400K | 1M (beta) |
Tier gratuito: 60 richieste/min, 1M token/min, nessuna carta di credito richiesta.
Funzionalità Chiave
- Finestra di contesto 1M token — GA, stesso prezzo del contesto standard
- Custom tools (function calling) — dichiarare funzioni esterne che Gemini può chiamare
- Grounding con Google Search — risultati di ricerca in tempo reale
- Multimodale nativo — testo, immagini, audio e video in una singola richiesta
Esempio di Codice: Custom Tools
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"),
"unit": types.Schema(type=types.Type.STRING, enum=["celsius", "fahrenheit"]),
},
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]),
)
Confronto API: Gemini 3.1 Pro vs GPT-5.2 vs Claude Sonnet 4.6
| Funzionalità | Gemini 3.1 Pro | GPT-5.2 | Claude Sonnet 4.6 |
|---|---|---|---|
| Prezzo input | $2/1M | $5/1M | $3/1M |
| Contesto | 1M (GA) | 400K | 1M (beta) |
| Multimodale | Testo+immagine+audio+video | Testo+immagine+audio | Testo+immagine |
| Coding (SWE-bench) | 76.8% | 80.0% | 79.6% |
Integrazione con Y Build
// 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));
}
Conclusione
Gemini 3.1 Pro è la migliore API frontier per rapporto qualità-prezzo a marzo 2026. Per gli sviluppatori: usa Gemini per multimodale e risparmio, Claude per coding e agenti, e GPT-5.2 per ragionamento matematico.
Stai costruendo un prodotto AI? Y Build gestisce l'intero stack. Inizia gratis.
Fonti:
Be first to build with AI
Y Build is the AI-era operating system for startups. Join the waitlist and get early access.