First Steps with the Gemini API
Install google-genai SDK, create a client, make your first generate_content call, explore system instructions, count_tokens, temperature parameters, and multimodal image input.
Write a script that accepts an image and a question via CLI, counts tokens before sending, calls generate_content with system instruction 'You are a concise technical expert', and prints the response.
Copy and adapt to your context. Text in angle brackets should be replaced.
from google import genai
from google.genai import types
client = genai.Client(api_key=GEMINI_API_KEY)
MODEL_ID = "gemini-2.5-flash"
response = client.models.generate_content(
model=MODEL_ID,
contents="Explain the difference between a list and a tuple in Python in 3 sentences.",
config=types.GenerateContentConfig(
system_instruction="You are a concise Python mentor.",
temperature=0.3,
)
)
print(response.text)
print(f"Tokens: {response.usage_metadata.total_token_count}")Hardcoding the API key in source code; reading response.text without checking finish_reason (throws an exception on SAFETY blocks); ignoring usage_metadata when estimating costs.