jsantisi Avatar

Juan Santisi

Solutions, Evals & Agile Lessons

Can a 12B model running on a laptop write Apex you would actually deploy? I spent an evening finding out, and the answer surprised me: yes, but only after I stopped feeding it Salesforce’s official agent skills and gave it 380 tokens of rules aimed at the mistakes it actually made.

I ran the same trigger prompt four ways on one local model, then tried a prompt it had never seen and a 120B model through OpenRouter. The most complete instructions produced code that did not compile. The shortest targeted ones produced correct code with six valid tests, at no extra cost in speed.

If you are building Salesforce tooling on smaller or self-hosted models, or wiring documentation into an MCP server, how you feed knowledge to the model matters as much as which model you pick.

The setup

Everything ran on a Razer 14 laptop with a laptop RTX 5070 (8 GB of VRAM, 95 W power cap) using Ollama on Windows 11.

  • Model: gemma4:12b, 11.9B parameters, Q4_K_M quantization, fully on the GPU at about 6.9 GB of VRAM.
  • Settings: 32K context, thinking turned off (--think=false), temperature 0.2 for the custom models.
  • Prompt: “Write an Apex trigger handler for Account that sets Rating to Hot when AnnualRevenue exceeds 1M, with a test class.”
  • Scoring: does it compile, does the logic work, and are the tests valid, meaning would each test catch a real bug.

One honest caveat up front: I scored the code by careful review, not by deploying it to an org. The bugs I describe are well-known Apex behaviors, but a scratch-org run is the next step.

Two housekeeping wins worth sharing: Gemma’s thinking mode is on by default and adds a long hidden reasoning pass, and having LM Studio running in the background alongside Ollama is an easy way to lose VRAM you need.

Round 1: four ways to prompt the same model

I ran the Account prompt with four different system prompts:

  1. Plain: no system prompt at all.
  2. Official skills: the platform-apex-generate and platform-apex-test-generate SKILL.md files from Salesforce’s open-source forcedotcom/sf-skills library (Apache-2.0), loaded through an Ollama Modelfile.
  3. Handwritten v1: about 350 tokens of Apex standards I wrote: trigger structure, oldMap change checks, null checks, the Assert class, and required test categories.
  4. Handwritten v2: v1 plus one sentence added after I saw how v1 failed.
RunPrompt tokensPrompt timeGenerationTotalCompilesWorksValid tests
Plain380.5 s31.0 tok/s31 sYesYesMostly
Official skills9,2499.0 s28.7 tok/s75 sNoNo0 of 3
Handwritten v13500.6 s32.9 tok/s31 sYesNo1 of 6
Handwritten v23780.6 s33.4 tok/s33 sYesYes6 of 6

The official skills more than doubled the response time and produced the worst code. The plain model produced working code, but only because it did less. The handwritten v2 run was the only one that was correct and fully tested, and a second run gave the same result.

What broke

The failures are more useful than the scores, because each one is a mistake a junior developer could make too.

The official skills run copied the look of good code, not the substance. It used Trigger Action naming, Given/When/Then comments, @TestSetup, the Assert class, a 251-record bulk test, and a phased status report straight out of the skill. Underneath:

  • The action class implemented TA_Account_Action, an interface that does not exist anywhere, so nothing compiles.
  • It read Trigger.newMap in a before insert trigger, where it is null. Every Account insert in the org would throw.
  • It invented a rule that only set Hot when revenue increased, so an account dropping from 3M to 2M never qualified.
  • It claimed to “invoke” the test-generation skill and declared the code “ready for deployment” while admitting it could not run Code Analyzer or tests.

Handwritten v1 tried harder and tripped on two classics.

  • Its change check was inverted: !isNew || ... oldMap.get(acc.Id) calls get() on a null map during insert. One character crashed every insert with revenue.
  • Five of six tests asserted on the in-memory record after DML. A before trigger changes the record being saved, not your test’s variable, so those tests either failed or passed no matter what the code did.

One added sentence, “After DML, re-query records before asserting; before-trigger changes are not reflected on the in-memory variable,” fixed every test in v2, and the inverted logic did not come back.

Round 2: an unseen prompt and a 120B model

A prompt you tuned against is not a fair test, so I switched to one the rules had never seen: “Write an Apex trigger handler for Opportunity that creates a follow-up Task for the Opportunity owner when StageName changes to Closed Won, due 7 days later, with a test class.” This one needs an after trigger, related-record DML, and duplicate protection.

The local gemma v2 model generalized well: after update trigger, a correct oldMap change check, one Task insert outside the loop, the right Task fields, and tests that re-queried. Four of five tests were valid.

Then I ran the same prompt on NVIDIA’s Nemotron 3.5 Super VL (120B total parameters, 12B active) through OpenRouter, using the OpenClaw agent’s one-shot agent exec mode, with the same v2 standards in the message.

gemma v2 (local)Nemotron v2 (OpenRouter)
Logic correctYesYes
Duplicate-Task test (already Closed Won)MissingPresent
Task OwnerId assertedNoYes
Valid tests4 of 55 of 6, setup at risk
Input tokens39026,646
Cost$0$0.0056
Time39 snot measured

The bigger model wrote more thorough tests, but both models made the same mistake: a “null StageName” test that sets a required field to null, which throws REQUIRED_FIELD_MISSING. Nemotron also created test users named testuser0@example.com. Salesforce usernames are unique across all orgs, so that setup is likely to fail with a duplicate-username error and take every test down with it.

Most of Nemotron’s 26K input tokens were OpenClaw’s own system prompt and tool definitions, not my standards.

What I learned

  1. Short and targeted beats long and general for small models. 378 tokens aimed at real failures outperformed 9,249 tokens of official guidance.
  2. Agent skills assume a feedback loop. The sf-skills are written for agents that compile, run Code Analyzer and execute tests, so mistakes get caught and fixed. Pasted into a plain chat prompt, there is no loop, and the model imitates the patterns without being checked. That is a mismatch of use, not a flaw in the skills.
  3. More instruction raises ambition and error rate together. Guidance pushed the model to attempt oldMap checks and edge-case tests. It got them wrong until a specific rule corrected the specific mistake.
  4. One sentence per failure mode works, but a broad rule can create new failures. “Null-check every field” caused both models to write invalid tests on required fields. Rules need to be precise.
  5. A bigger model is not a substitute for good rules. Nemotron wrote better tests, yet repeated the rule-induced bug.
  6. Laptop throughput is fine for this. 31 to 34 tokens per second for a 12B model is faster than you read. A desktop GPU would buy you more VRAM for bigger models more than it would buy speed.

The prompt I use now

Here is the v2 Modelfile that produced the correct Account code. Save it as Modelfile, then run:

ollama create apex-gemma -f Modelfile and ollama run apex-gemma --think=false "your prompt".

FROM gemma4:12b
PARAMETER temperature 0.2
PARAMETER num_ctx 32768
SYSTEM """
You are a senior Salesforce developer. Follow these Apex standards in every answer:
Triggers
- One trigger per object; the trigger only delegates to a handler class.
- Use before triggers to modify fields on the triggering records; after triggers for related records.
- On update, compare against Trigger.oldMap and only process records whose relevant fields changed.
- No SOQL or DML inside loops. Collect IDs, query once, update once.
- Null-check every field before comparing it.
- Handler classes declare sharing explicitly (with sharing unless there is a stated reason).
Requirements
- If a requirement is ambiguous (e.g. should a value be reset when the condition stops being true), state your assumption in one line before the code.
- Respect exact boundaries: "exceeds" means >, not >=.
Tests
- Use @isTest, @TestSetup where useful, never SeeAllData=true.
- Use the Assert class (Assert.areEqual, Assert.isNull), not System.assertEquals.
- Always cover: positive case, negative case, exact boundary value, null values, update path, and a bulk test of 200+ records.
- Wrap the action under test in Test.startTest()/Test.stopTest().
- After DML, re-query records before asserting; before-trigger changes are not reflected on the in-memory variable.
Output
- Order: assumptions, handler, trigger, test class. Keep explanations brief.
"""

Based on Round 2, these are the changes I am testing next. They are not yet verified:

  • Replace the null-check line with: “Null-check optional fields; never set required fields (Name, StageName, CloseDate) to null in tests.”
  • Add: “Test users need globally unique usernames, e.g. append a timestamp or the org Id.”
  • Add: “Query reference data (Profiles, RecordTypes) once, outside loops, in test setup too.”
  • Add: “Prefer status flags such as IsWon and IsClosed over hard-coded picklist labels.”

What this means for Salesforce MCP servers

I am building SfCognitio-mcp, an open-source (Apache-2.0) Salesforce knowledge server meant to make any agent a Salesforce expert. This experiment changed how I plan to feed it knowledge: in layers, sized to the model that is asking.

LayerWhat it holdsWhen it is served
Core rules300 to 600 tokens of distilled standards per domain (triggers, tests, SOQL, LWC)Always
Failure-mode rulesOne sentence per bug seen in evaluationAppended to that domain’s core rules
Skill referenceOfficial sf-skills, chunked by sectionOn demand; whole skills only for large models
Developer docsApex, LWC, Platform Events and API docsRetrieval (RAG) results
Evaluation suitePrompts plus a rubric: compiles, works, tests validBefore any rule change ships

The design rules that fall out of this:

  • Default to core and failure-mode rules for models around 12B. Full skills overwhelm them.
  • Every bug found in review becomes one rule plus one evaluation prompt that reproduces it.
  • Strip agent-only steps (CLI commands, scripts, analyzer calls) from anything served to a model that cannot run tools.
  • Keep the Apache-2.0 attribution when bundling sf-skills content.

If you maintain a Salesforce MCP server, consider exposing something like get_standards(domain, model_tier) alongside your search tool, so the client can pick concise rules or full reference depending on the model.

Caveats and what’s next

This was one evening, two prompts, and a handful of runs, so treat it as a field report, not a benchmark.

  • Review, not execution. Correctness was judged by code review. Deploying to a scratch org and running sf apex run test is next.
  • Temperature 0.2 is near-deterministic. Repeats confirm stability, not robustness; more prompts test robustness.
  • The Nemotron baseline was not clean. The no-standards run executed in a folder containing my Modelfile, and the agent read it, so I only report the run with standards.
  • The skills were used outside their intended setting. sf-skills target agents with tools. In an agent loop with a compiler and tests, the result may be very different.

Next I will apply the revised rules, rerun both prompts locally, add a scratch-org test loop, and try Nemotron 3 Nano 30B, which should fit in 24 GB of VRAM on a desktop.

If you have run similar experiments with Apex and small models, I would like to compare notes.

Leave a Reply