Google Gemini 2.5 Pro and Flash offer long context windows, multimodal inputs, and competitive pricing. This guide covers getting started with the Gemini API, function calling, and using the 1M token context window effectively.
Get a free Gemini API key at ai.google.dev. The free tier is generous — Gemini 2.0 Flash is completely free up to rate limits. For production, add billing at console.cloud.google.com and use Gemini 2.5 Pro for complex tasks or Flash for speed and cost efficiency.
Gemini 2.5 Flash is typically the best choice for most applications: it processes 1M tokens of context at a fraction of GPT-4o's price, supports multimodal inputs, and achieves strong benchmark scores. Reserve 2.5 Pro for tasks that need deeper reasoning.
Python: `pip install google-genai`. Then: `from google import genai; client = genai.Client(api_key='YOUR_API_KEY'); response = client.models.generate_content(model='gemini-2.5-flash', contents='What is the capital of France?'); print(response.text)`.
Node.js: `npm install @google/genai`. Then: `const { GoogleGenerativeAI } = require('@google/genai'); const genai = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); const model = genai.getGenerativeModel({model: 'gemini-2.5-flash'}); const result = await model.generateContent('Hello'); console.log(result.response.text())`.
Gemini 2.5 Pro and Flash both support up to 1 million tokens of context — enough for an entire codebase, a book, or hours of meeting transcripts. Pass large documents directly in the prompt: no chunking or RAG required for most documents.
For very large inputs, use the Files API to upload once and reference repeatedly: `file = client.files.upload(path='large_document.pdf'); response = client.models.generate_content(model='gemini-2.5-flash', contents=[file, 'Summarise the key arguments'])`. Uploaded files are stored for 48 hours.
Define tools as Python functions with type annotations and docstrings. Gemini infers the JSON Schema from the type hints: `def get_weather(city: str) -> dict: '''Get current weather for a city. Args: city: The city name.''' ...`. Pass the function to `tools=[get_weather]` in the generate call.
Gemini supports parallel function calling — it can request multiple tool calls in a single turn. Handle the response by checking `response.candidates[0].content.parts` for `function_call` parts, executing each, and returning results as `function_response` parts.
Stream responses: `for chunk in client.models.generate_content_stream(model='gemini-2.5-flash', contents='Write a poem'): print(chunk.text, end='', flush=True)`.
For multi-turn conversations, use a chat session which manages history automatically: `chat = client.chats.create(model='gemini-2.5-flash'); response = chat.send_message('Hello'); response2 = chat.send_message('What did I just say?')`. The session remembers all previous turns.
A practical introduction to the OpenAI API covering authentication, the chat completions endpoint, streaming, error handling, and cost management — with working code in Python and JavaScript.
Read guideThe Anthropic API gives access to the Claude model family. This guide covers authentication, the Messages API, vision inputs, tool use, and the key differences from the OpenAI API format.
Read guide