Prompt Engineering in Google AI Studio
Master the three prompt types, learn to control model behavior with parameters and system instructions, and iterate your way to optimal results.
Freeform Prompts
Freeform prompts are the most flexible prompt type. You provide open-ended instructions and the model generates a response. They're ideal for:
- Content generation (articles, emails, stories)
- Summarization and analysis
- Code generation and explanation
- Creative brainstorming
- Question answering
# Example freeform prompt
You are a senior software engineer. Review the following code
and suggest improvements for readability, performance, and
error handling.
```python
def get_data(url):
r = requests.get(url)
return r.json()
```
Structured Prompts
Structured prompts use an examples table to teach the model a pattern. This is powerful for consistent, repeatable tasks:
| Input (Product Review) | Output (Category) | Output (Sentiment) |
|---|---|---|
| "The battery lasts forever, love this phone!" | Electronics | Positive |
| "Comfortable fabric but stitching came loose" | Clothing | Mixed |
| "Arrived broken, waste of money" | General | Negative |
The model learns from these examples and applies the same pattern to new inputs. Add 3-5 diverse examples for best results.
Chat Prompts
Chat prompts enable multi-turn conversations with persistent context. Key components:
- System instruction: Defines the model's role, personality, and constraints
- User messages: The human side of the conversation
- Model responses: AI-generated replies that maintain context
# System instruction example
You are a friendly cooking assistant. Help users find recipes
based on ingredients they have. Always suggest substitutions
for missing ingredients. Keep responses concise and practical.
Format recipes with clear numbered steps.
Model Parameters
Fine-tune model behavior using the settings panel:
| Parameter | Range | Effect | Use When |
|---|---|---|---|
| Temperature | 0 - 2 | Controls randomness. Lower = more focused, higher = more creative | Low for factual tasks, high for creative writing |
| Top-K | 1 - 40 | Limits token selection to top K candidates | Lower for precise outputs, higher for variety |
| Top-P | 0 - 1 | Nucleus sampling - considers tokens with cumulative probability P | 0.8-0.95 for most tasks |
| Max output tokens | 1 - 8192+ | Maximum length of the response | Set based on expected response length |
Safety Settings
Google AI Studio provides configurable safety filters for several categories:
- Harassment: Filter content that targets individuals or groups
- Hate speech: Filter discriminatory or hateful content
- Sexually explicit: Filter adult content
- Dangerous content: Filter harmful or dangerous instructions
Each category can be set to: Block none, Block few, Block some, or Block most. The default settings are appropriate for most use cases.
Output Format Control
You can instruct the model to produce output in specific formats:
- JSON: Ask for structured JSON output with a specific schema
- Markdown: Tables, lists, headers for formatted content
- CSV: Comma-separated values for data processing
- Code: Specify the programming language and format requirements
# Requesting JSON output
Analyze the following text and return a JSON object with:
- "sentiment": "positive", "negative", or "neutral"
- "confidence": a number between 0 and 1
- "key_topics": an array of main topics mentioned
Text: "The new restaurant downtown has amazing pasta
but the service was quite slow during peak hours."
The Prompt Gallery
Google AI Studio includes a Prompt Gallery with pre-built prompt templates organized by category. Use these as starting points and customize them for your specific needs. Categories include marketing, coding, education, creative writing, and data analysis.
Testing and Iteration
Effective prompt engineering is an iterative process:
- Start simple: Begin with a basic prompt
- Evaluate output: Check if the response meets your requirements
- Add specificity: Include more constraints, examples, or context
- Adjust parameters: Tweak temperature, top-K, and top-P
- Compare models: Try the same prompt with different Gemini models
- Save and version: Save successful prompts to your library
💡 Try It: Build a Structured Prompt
Create a structured prompt that classifies customer support tickets into categories (billing, technical, account, general). Add 4-5 examples, then test with new inputs. Experiment with the temperature setting to see how it affects classification consistency.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX