💡 TL;DR - This is the construction manual for our Gemma 4 article. It gives you two concrete build tracks: a campaign copilot with function calling and chat memory, and a search visibility agent with multimodal SERP analysis. Both can be built on Gemma 4, run under your own data controls, and be shaped into a submission for the Gemma 4 Good Hackathon.
🗺️ How to Navigate This Page
- Read the companion article first - Gemma 4 - The Open Model That Turns Marketing Teams Into Agent Builders. That piece is the strategic map. This page is the build plan.
- Pick a build track - Track A is the more accessible starting point; Track B is the stronger technical stretch.
- Set up Gemma 4 locally or in a managed environment - use the quickstart below before the build sprint starts.
- Build the smallest working version first - prove tool calling or SERP analysis end to end before adding fine-tuning.
- Use the reference pack at the bottom - it contains the model docs, fine-tuning guides, and marketing-adjacent examples.
- If you want an external deadline, build toward the hackathon - both tracks map cleanly to the Gemma 4 Good Hackathon.
🏗️ Environment Quickstart
Before the build week, get Gemma 4 running in at least one environment you control. For most teams, the best starting point is the 26B A4B MoE model: it gives you strong reasoning with lower active-parameter cost than a dense model.
Option 1 - Ollama
Fastest path to a working local prototype.
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Recommended starting point
ollama run gemma4:26b
# Smaller edge variant
ollama run gemma4:e2b
# Dense variant for maximum quality experiments
ollama run gemma4:31b
Option 2 - Hugging Face Transformers
Use this path if you want direct programmatic control or plan to fine-tune.
pip install --upgrade transformers torch
from transformers import AutoProcessor, AutoModelForImageTextToText
MODEL_ID = "google/gemma-4-26b-it"
model = AutoModelForImageTextToText.from_pretrained(
MODEL_ID,
dtype="auto",
device_map="auto",
)
processor = AutoProcessor.from_pretrained(MODEL_ID)
Option 3 - Unsloth
Use this if your goal is efficient LoRA fine-tuning on commodity hardware.
curl -fsSL https://unsloth.ai/install.sh | sh
Then open http://localhost:8888, choose a Gemma 4 model, attach a dataset, adjust hyperparameters, and train.
Option 4 - Google AI Studio
Use Google AI Studio if you want to prototype prompts and structured outputs before you commit to a local runtime.
🧠 Core Concepts You Need
Function calling
Gemma 4 supports native function calling. You define tools as JSON schemas or Python functions, the model chooses when to invoke them, and your application executes the function and returns the result to the model.
For marketing agents, this is the base pattern for:
- pulling campaign metrics
- checking landing pages
- retrieving creative assets
- validating compliance rules
- comparing current observations against historical baselines
Example tool definition
get_campaign_metrics_tool = {
"type": "function",
"function": {
"name": "get_campaign_metrics",
"description": "Retrieve performance metrics for a specific campaign",
"parameters": {
"type": "object",
"properties": {
"campaign_id": {
"type": "string",
"description": "Campaign identifier"
},
"metric": {
"type": "string",
"enum": ["CTR", "CVR", "ROAS", "CPA"]
},
"date_range": {
"type": "string",
"description": "Time window, for example last_7_days"
}
},
"required": ["campaign_id", "metric"]
}
}
}
Chat memory
Gemma 4 is not stateful on its own. Your application must manage conversation history and decide what stays in context.
conversation = []
def chat(user_message, tools=None):
conversation.append({"role": "user", "content": user_message})
inputs = processor.apply_chat_template(
conversation,
tools=tools,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt"
)
output = model.generate(**inputs.to(model.device), max_new_tokens=512)
response = processor.decode(
output[0][len(inputs["input_ids"][0]):],
skip_special_tokens=True
)
conversation.append({"role": "assistant", "content": response})
return response
In practice, you should add:
- a sliding context window
- periodic summarisation
- a structured memory layer for longer-running workflows
LoRA fine-tuning
LoRA lets you specialise Gemma 4 on your own marketing tasks without retraining the base model.
| Goal | Model | Approach | Hardware |
|---|---|---|---|
| Quick prototype | E4B | QLoRA (4-bit) | Free Colab T4 or any 12 GB GPU |
| Production copilot | 26B A4B MoE | 16-bit LoRA | A100-class GPU |
| Maximum quality | 31B Dense | QLoRA (4-bit) | Single 24 GB GPU |
Recommended starting parameters:
r = 16
lora_alpha = 32
lora_dropout = 0.05
target_modules = [
"q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj"
]
learning_rate = 2e-4
num_train_epochs = 3
per_device_train_batch_size = 4
gradient_accumulation_steps = 4
warmup_ratio = 0.1
lr_scheduler_type = "cosine"
For the 26B A4B MoE model, prefer load_in_16bit=True over 4-bit QLoRA unless you have validated a stable quantised pipeline. MoE plus aggressive quantisation is where many build attempts break.
🛠️ Build Track A - Campaign Copilot
Goal: Ship a chat-based marketing assistant that answers campaign questions, calls tools to retrieve live data, and returns structured recommendations without taking destructive actions on its own.
This is the best first build for most teams because it gives immediate operational value without requiring full automation.
| Step | Deliverable | Detail |
|---|---|---|
| 1 | Local runtime working | Run Gemma 4 locally or via a managed endpoint. Prove basic tool calling with one test tool. |
| 2 | Define 3-5 marketing tools | Start with get_campaign_metrics, get_ad_creative, check_landing_page, generate_copy_variant, flag_compliance_issue. |
| 3 | Add memory wrapper | Support multi-turn analysis with a context window and session history. |
| 4 | Fine-tune if needed | Train a LoRA adapter on campaign brief-to-recommendation examples. |
| 5 | Add orchestration layer | Log tool calls, validate arguments, enforce approval for risky actions. |
| 6 | Record a short demo | Show question -> tool call -> structured recommendation with rationale. |
Suggested training record
{
"input": {
"role": "Campaign performance analyst",
"context": {
"brand": "Brand X",
"channel": "Meta Ads",
"audience": "US women 25-34 retargeting",
"objective": "Summer skincare collection launch",
"budget": "$50K/month",
"current_metrics": {
"CTR": 0.018,
"CVR": 0.031,
"ROAS": 2.4
}
},
"question": "CTR dropped 15% week over week. What should we investigate?"
},
"output": {
"diagnosis": [
"Check creative fatigue",
"Review audience overlap between ad sets",
"Inspect landing page load time"
],
"recommended_actions": [
"Pull frequency report for top ad sets",
"Run audience overlap analysis",
"Test landing page speed"
],
"tools_to_invoke": [
"get_campaign_metrics",
"check_landing_page"
],
"confidence": "medium"
}
}
Good sources of fine-tuning data
- historical campaign briefs paired with analyst recommendations
- performance review decks with action items
- QA checklists with pass/fail outcomes
- compliance review logs
- negative examples showing bad diagnosis, bad tone, or unsafe action selection
Start with 200 strong examples, not 2,000 noisy ones.
🛠️ Build Track B - Search Visibility Agent
Goal: Ship an agent that reads rendered search results pages, detects AI-generated summary blocks, checks whether your brand is cited, and produces a structured visibility report.
This is the more ambitious build because it combines vision, structured outputs, and tool-backed monitoring.
| Step | Deliverable | Detail |
|---|---|---|
| 1 | SERP screenshot pipeline | Capture full-page results consistently with Playwright or Puppeteer. |
| 2 | Gemma 4 vision analysis | Feed screenshots to the model and ask for structured extraction. |
| 3 | JSON report schema | Return a stable report shape with brand citations, positions, and recommendations. |
| 4 | Historical comparison tools | Compare current results with prior snapshots and thresholds. |
| 5 | Fine-tune if needed | Train on screenshot plus ground-truth JSON pairs. |
| 6 | Record a short demo | Show capture -> analysis -> report -> comparison against baseline. |
Multimodal prompting pattern
from PIL import Image
image = Image.open("serp_screenshot.png")
messages = [
{
"role": "user",
"content": [
{"type": "image", "image": image},
{
"type": "text",
"text": """Analyse this search results page and return JSON with:
1. ai_overview_present
2. ai_overview_brands
3. organic_results
4. paid_results
5. recommendations
Focus on whether [BRAND_NAME] appears in the AI-generated summary or citations."""
}
]
}
]
For multimodal prompting, place the image before the text instruction. That ordering matters.
🏁 Hackathon Path - Gemma 4 Good
Both tracks can be turned into a submission for the Gemma 4 Good Hackathon.
| Requirement | How the build tracks satisfy it |
|---|---|
| Working demo | Both tracks produce a live agent with model inference and tool use |
| Public code repository | Publish the project with a clear README and architecture note |
| Technical write-up | Document your model choice, tool layer, and evaluation method |
| Short video | Use the demo you create in Step 6 |
Hackathon details
- Prize pool: $200,000
- Deadline: May 18, 2026, 23:59 UTC
- Judging: Technical Execution (40%), Innovation (30%), Potential Impact (20%), Presentation (10%)
- Competition link: Gemma 4 Good Hackathon on Kaggle
If you frame a marketing agent for submission, be precise about the impact case. Examples that are easier to defend:
- helping small businesses monitor visibility in AI-mediated search
- reducing compliance failures in regulated advertising
- building privacy-preserving internal copilots that keep sensitive data local
⚠️ Guardrails and Governance
Both tracks need a harness around the model. The model generates. The harness constrains.
Approval gates
Do not give the model unrestricted access to destructive operations such as:
- pausing ads
- changing bids
- publishing content
- modifying production tracking
High-risk actions should require explicit approval.
Separate compliance from generation
Do not ask the same model instance that generated the content to be the final compliance authority. Use:
- a second validation model, or
- a deterministic rules layer
This is the same separation-of-concerns pattern we outlined in our security harnesses guide.
Training-data hygiene
Review every training example for:
- PII
- client-sensitive information
- customer identifiers
- regulated claims
Fine-tuning on dirty data creates avoidable legal and operational risk.
Tool-call validation
Always validate model-generated arguments before execution. Check:
- required fields
- types
- enum values
- account or campaign scope
- safe action class
Do not let malformed tool calls reach live systems.
🤔 Discussion Prompts
- Which marketing workflow is the best fit for a locally running open model?
- What does a good fine-tuning example look like for a campaign copilot?
- What accuracy threshold would you require before trusting a search visibility agent to alert automatically?
- How do you measure whether a copilot is saving analyst time rather than creating new review burden?
- Should you build one general Gemma 4 agent or several specialised agents for SEO, paid media, analytics, and creative QA?
Add your thoughts in the comments or in our internal #ai-knowledge-hub channel.
📚 Reference Pack
Gemma 4 - Official
- Gemma 4 announcement - Google DeepMind
- Gemma 4 model card - Google AI for Developers
- Function calling with Gemma 4 - Google AI for Developers
- Gemma 4 on Google Cloud
- Gemma 4 in Android AICore Developer Preview
- Gemma 4 on Hugging Face
Fine-tuning
- Gemma 4 fine-tuning guide - Unsloth
- Fine-tune Gemma 4 with LoRA and QLoRA - Lushbinary
- How to fine-tune Gemma 4 with Unsloth - Avenchat
- From OOM Errors to Working Model - Gabriel Preda
Function calling and agents
- Building agentic systems with Gemma 4 - GemiLab
- Tool calling with Gemma 4 and Python - Machine Learning Mastery
- Build AI agent with Gemma 4: function calling and MCP - Lushbinary
- How to use Gemma 4 with the Gemini API - Philipp Schmid
Marketing-adjacent patterns
- Three MarTech solutions - Google Developers Blog
- Google Marketing Solutions repositories
- Copycat - Google Marketing Solutions
- ViGenAiR - Google Marketing Solutions
From our series
- Gemma 4 - The Open Model That Turns Marketing Teams Into Agent Builders
- The Muscles of the Machine - Tools, MCP, and CLI
- The Anatomy of a Marketing Agent
- Code Maintenance and Security Harnesses
- The Empowerment Imperative
Hackathon
🚀 Ready? Get Gemma 4 running first. Pick one build track. Prove the smallest useful workflow end to end. Add fine-tuning only after the base harness works. The open weights matter only if they become a working system.
Discussion & Idea Voting
Up-vote next week’s build idea by reacting with 👍 to any comment.
Discussion & Idea Voting
Up-vote next week’s build idea by reacting with 👍 to any comment.
Published on Tuesday, April 14, 2026