SfSkills Domains Docs GitHub

Home / agentforce / agentforce-cost-optimization

agentforce-cost-optimization

Use when Agentforce run costs are climbing, you need to forecast scale, or you want to reduce tokens per conversation without hurting quality.

Open the skill folder on GitHub


Agentforce Cost Optimization

Agentforce cost looks like "we'll just pay per run" right up until volume meets reality. A customer-service agent handling 200,000 conversations/month can consume 10× the tokens of a well-tuned version of the same agent — same quality, same subagents, different token discipline. The cost drivers are predictable: subagent instruction length, prompt template verbosity, grounding payload size, tool-call round-trips, and model tier. None of these are free to change, but they all respond to focused work.

The job is to measure first, then optimize the top three contributors. Most orgs find that subagent instructions and grounding dominate — often 60-80% of tokens per conversation. Once those are disciplined, the remaining optimizations (template reuse, model-tier selection) become viable.

Terminology. This skill leads with subagent because that is the current product term — Salesforce renamed agent topics to subagents in April 2026, with no change to functionality. It deliberately keeps topic in metadata and API names and in search keywords, because those did not change, and readers arriving with the older vocabulary still need to find this skill.


Before Starting

Core Concepts

What Tokens Are You Paying For?

Every conversation pays for:

  1. System prompt — the framework-level Agentforce prompt.
  2. Subagent instructions — active subagent's instructions injected verbatim.
  3. Prompt template — any custom template rendered per turn.
  4. Grounding — retrieved content from Data Cloud, Knowledge, or explicit variables.
  5. Conversation history — full turn history on each call.
  6. Tool output — action results returned into context.

The 80/20 Rule

For most agents, subagent instructions + grounding = 60-80% of token spend. Conversation history grows linearly in long sessions. Tool output is lumpy but occasionally large (SOQL result sets dumped raw into context).

Reducing Subagent Instruction Tokens

Reducing Grounding Tokens

Conversation History Discipline

Long sessions inflate every turn's token count. Patterns:

Model Tier Selection

Not every action needs the most capable model. Use tiered routing:

Caching Opportunities


Common Patterns

Pattern 1: Subagent Instruction Audit And Trim

Per subagent, measure instruction token count. Target 150-300 tokens per subagent instruction. Trim anything above 500 without a compelling reason.

Pattern 2: k-3 Retriever With Reranker

Retrieve 10 candidates; rerank; inject top 3. Cuts grounding tokens 70% vs retrieve-10-inject-10.

Pattern 3: Conversation Summarization Trigger

After N turns or M tokens of history, replace older turns with a one-line summary.

Pattern 4: Tiered Model Routing

Route classification / intent steps to a smaller model; reasoning/response to the capable model.

Pattern 5: Tool Output Projection

When a tool returns a large payload (e.g. SOQL result), project the fields the agent actually needs instead of dumping the full response.


Decision Guidance

SituationRecommended ApproachReason
Token usage high, unknown contributorInstrument and measure firstAvoid guessing
Subagent instructions > 500 tokensTrim (Pattern 1)Biggest win
Grounding k ≥ 5 without evaluationReduce k + rerank (Pattern 2)Second biggest win
Long conversationsSummarize (Pattern 3)Linear savings per turn
Classification step using largest modelSwitch to smaller tier (Pattern 4)Cheap wins
Tool returns wide recordsProject fields (Pattern 5)Eliminates silent waste

Review Checklist

  1. Measure — 7 days of run data broken down by token source.
  2. Identify top 3 contributors.
  3. Optimize subagent instructions first.
  4. Optimize grounding second.
  5. Add conversation summarization if sessions are long.
  6. Apply tier routing where quality allows.
  7. Re-measure; document cost savings.

Salesforce-Specific Gotchas

  1. Trust Layer adds tokens — masking, citation, guardrails all add context weight.
  2. Grounding sources can include large boilerplate (Knowledge article footers); index selectively.
  3. Tool output is counted even if the agent ignores it.
  4. Managed subagents may have opaque instruction length; audit via runtime logs.
  5. Switching model tier changes quality — do not do this without A/B evaluation.

Proactive Triggers

Output Artifacts

ArtifactDescription
Cost modelTokens per conversation by contributor
Optimization planPrioritized trim list with expected savings
Tier routing designStep → model mapping

Package files