observing-agentforce
Original:🇺🇸 English
Translated
Analyze production Agentforce agent behavior using session traces and Data Cloud. TRIGGER when: user queries STDM session data or Data Cloud trace records; investigates production agent failures, regressions, or performance issues; asks about session traces, conversation logs, or agent metrics; wants to reproduce a reported production issue in preview; runs findSessions or trace analysis queries. DO NOT TRIGGER when: user creates, modifies, or debugs .agent files during development (use developing-agentforce); writes or runs test specs (use testing-agentforce); uses sf agent preview for local development iteration; deploys or publishes agents.
9installs
Sourceforcedotcom/sf-skills
Added on
NPX Install
npx skill4agent add forcedotcom/sf-skills observing-agentforceTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Agentforce Observability
Improve Agentforce agents using session trace data and live preview testing.
Three-phase workflow:
- Observe -- Query STDM sessions from Data Cloud (if available), OR run test suites + preview with local traces as fallback
- Reproduce -- Use to simulate problematic conversations live
sf agent preview - Improve -- Edit the file directly, validate, publish, verify
.agent
Platform Notes
- Shell examples below use bash syntax. On Windows, use PowerShell equivalents or Git Bash.
- Replace with
python3on Windows.python - Replace with
/tmp/(PowerShell) or$env:TEMP\(cmd).%TEMP%\ - Replace with
jqif jq is not installed.python -c "import json,sys; ..."
Routing
Gather these inputs before starting:
- Org alias (required)
- Agent API name (required for preview and deploy; ask if not provided)
- Agent file path (optional) -- path to the file, typically
.agent. Auto-detect if not provided.force-app/main/default/aiAuthoringBundles/<AgentName>/<AgentName>.agent - Session IDs (optional) -- analyze specific sessions; if absent, query last 7 days
- Days to look back (optional, default 7)
Determine intent from user input:
- No specific action -> run all three phases: Observe -> surface issues -> ask if user wants to Reproduce and/or Improve
- "analyze" / "sessions" / "what's wrong" -> Phase 1 only, then suggest next steps
- "reproduce" / "test" / "preview" -> Phase 2 (run Phase 1 first if no issues in hand)
- "fix" / "improve" / "update" -> Phase 3 (run Phase 1 first if no issues in hand)
Resolve agent name
Before any STDM query, resolve the user-provided agent name against the org to get the exact and :
MasterLabelDeveloperNamebash
sf data query --json \
--query "SELECT Id, MasterLabel, DeveloperName FROM GenAiPlannerDefinition WHERE MasterLabel LIKE '%<user-provided-name>%' OR DeveloperName LIKE '%<user-provided-name>%'" \
-o <org>- = display name used by STDM
MasterLabeland Agent Builder UI (e.g. "Order Service")findSessions - = API name with version suffix used in metadata (e.g. "OrderService_v9")
DeveloperName - The flag for
--api-nameusessf agent preview/activate/publishwithout theDeveloperNamesuffix (e.g. "OrderService")_vN
Store these values:
- -- for
AGENT_MASTER_LABELagent filterfindSessions() - --
AGENT_API_NAMEwithoutDeveloperNamesuffix, for_vNCLI commandssf agent - -- the Salesforce record ID for this agent
PLANNER_ID
Locate the .agent file
Step 1 -- Search locally:
bash
find <project-root>/force-app/main/default/aiAuthoringBundles -name "*.agent" 2>/dev/nullIf the user provided an agent file path, use that directly. Otherwise, search for files matching .
AGENT_API_NAMEStep 2 -- If not found locally, retrieve from the org:
bash
sf project retrieve start --json --metadata "AiAuthoringBundle:<AGENT_API_NAME>" -o <org>Known bug:creates a double-nested path:sf project retrieve start. Fix it immediately after retrieve:force-app/main/default/main/default/aiAuthoringBundles/...
bash
if [ -d "force-app/main/default/main/default/aiAuthoringBundles" ]; then
mkdir -p force-app/main/default/aiAuthoringBundles
cp -r force-app/main/default/main/default/aiAuthoringBundles/* \
force-app/main/default/aiAuthoringBundles/
rm -rf force-app/main/default/main
fiStep 3 -- Validate the retrieved file:
Read the file and verify it has proper Agent Script structure:
.agent- block with
system:instructions: - block with
config:developer_name: - or
start_agentblocks withsubagentreasoning: instructions: - Each subagent should have distinct content (not identical across subagents)
instructions:
Store the resolved path as for Phase 3.
AGENT_FILEPhase 0: Discover Data Space
Before running any STDM query, determine the correct Data Cloud Data Space API name.
bash
sf api request rest "/services/data/v63.0/ssot/data-spaces" -o <org>Note: is a beta command -- do not add (that flag is unsupported and causes an error).
sf api request rest--jsonThe response shape is:
json
{
"dataSpaces": [
{
"id": "0vhKh000000g3DjIAI",
"label": "default",
"name": "default",
"status": "Active",
"description": "Your org's default data space."
}
],
"totalSize": 1
}The field is the API name to pass to .
nameAgentforceOptimizeServiceDecision logic:
- If the command fails (e.g. 404 or permission error), fall back to and note it as an assumption.
'default' - Filter to only entries.
status: "Active" - If exactly one active Data Space exists, use it automatically and confirm to the user: "Using Data Space: ".
<name> - If multiple active Data Spaces exist, show the list (label + name) and ask the user which to use.
Store the selected value as for all subsequent steps.
nameDATA_SPACEPrerequisite check: STDM DMOs
After deploying the helper class (step 1.0), run a quick probe to verify the STDM Data Model Objects exist in Data Cloud:
bash
sf apex run -o <org> -f /dev/stdin << 'APEX'
ConnectApi.CdpQueryInput qi = new ConnectApi.CdpQueryInput();
qi.sql = 'SELECT ssot__Id__c FROM "ssot__AiAgentSession__dlm" LIMIT 1';
try {
ConnectApi.CdpQueryOutputV2 out = ConnectApi.CdpQuery.queryAnsiSqlV2(qi, '<DATA_SPACE>');
System.debug('STDM_CHECK:OK rows=' + (out.data != null ? out.data.size() : 0));
} catch (Exception e) {
System.debug('STDM_CHECK:FAIL ' + e.getMessage());
}
APEXIf : STDM is not activated. Inform the user and switch to Phase 1-ALT:
STDM_CHECK:FAILSTDM (Session Trace Data Model) is not available in this org. To enable: Setup -> Data Cloud -> Data Streams and verify "Agentforce Activity" is active. Proceeding with fallback: test suites + local traces.
If , proceed to Phase 1 (STDM path).
STDM_CHECK:OKPhase 1-ALT: Observe Without STDM (Fallback Path)
When STDM is not available, use test suites and with local trace analysis.
sf agent preview --authoring-bundle| Data source | When to use | Pros | Cons |
|---|---|---|---|
| STDM (Phase 1) | Historical production analysis | Real user data, volume | Requires Data Cloud, 15-min lag |
| Test suites + local traces (Phase 1-ALT) | Dev iteration, orgs without STDM | Instant, full LLM prompt, variable state | Preview only, no real user data |
1-ALT.1 Run existing test suite (if available)
bash
sf agent test list --json -o <org>
sf agent test run --json --api-name <TestSuiteName> --wait 10 --result-format json -o <org> | tee /tmp/test_run.json
JOB_ID=$(python3 -c "import json; print(json.load(open('/tmp/test_run.json'))['result']['runId'])")
sf agent test results --json --job-id "$JOB_ID" --result-format json -o <org>1-ALT.2 Derive test utterances from .agent file (if no test suite)
If no test suite exists, derive utterances: one per non-entry subagent (from keywords), one per key action, one guardrail test, one multi-turn test.
description:1-ALT.3 Preview with --authoring-bundle
(local traces)
--authoring-bundleRun each test utterance through preview to generate local trace files:
bash
sf agent preview start --json --authoring-bundle <BundleName> -o <org> | tee /tmp/preview_start.json
SESSION_ID=$(python3 -c "import json; print(json.load(open('/tmp/preview_start.json'))['result']['sessionId'])")
sf agent preview send --json --session-id "$SESSION_ID" --authoring-bundle <BundleName> \
--utterance "$UTT" -o <org> | tee /tmp/preview_response.json
sf agent preview end --json --session-id "$SESSION_ID" --authoring-bundle <BundleName> -o <org>Trace file location:
.sfdx/agents/{BundleName}/sessions/{sessionId}/traces/{planId}.json1-ALT.4 Local trace diagnosis
| Issue type | Trace command |
|---|---|
| Subagent misroute | |
| Action not called | |
| LOW adherence | |
| Variable capture fail | |
| Vague instructions | |
DefaultTopic trace quirk: With , the root field often shows even when routing works. Always use for the real subagent chain.
--authoring-bundle.topic"DefaultTopic"NodeEntryStateStep.data.agent_nameEntry answering directly (SMALL_TALK pattern): If trace shows grounding and transition tools visible but none invoked, add "You are a router only. Do NOT answer questions directly." to instructions.
start_agentSMALL_TALKstart_agent1-ALT.5 Classify and present
Classify issues using the categories in . After presenting findings, automatically proceed to agent config evidence analysis.
references/issue-classification.mdPhase 1: Observe -- Query STDM
Full STDM query details, Apex service deployment, and response parsing: seereferences/stdm-queries.md
1.0 Deploy helper class (once per org)
Deploy Apex class to the org. Check if already deployed first:
AgentforceOptimizeServicebash
sf data query --json --query "SELECT Id, Name FROM ApexClass WHERE Name = 'AgentforceOptimizeService'" -o <org>If not deployed, copy from skill directory and deploy. See for full steps.
references/stdm-queries.md1.1 Find sessions
Query recent sessions using . Parse from the Apex debug log. If returns empty, switch to Phase 1-ALT.
findSessions()DEBUG|STDM_RESULT:findSessions1.2 Get conversation details
Use for up to 5 sessions (most recent first). Returns turn-by-turn data with messages, steps, topics, and action results.
getMultipleConversationDetails()1.2b Get LLM prompt/response (optional)
When LOW adherence detected, use to get the actual LLM prompt and response.
getLlmStepDetails()1.2c Get aggregated metrics (recommended first step)
Use for high-level health dashboard: session rates, top intents, quality distribution, RAG averages.
getAggregatedMetrics()1.2d Get moment insights (per-session detail)
Use for intent summaries, quality scores (1-5), and retriever metrics per session.
getMomentInsights()1.2e Run observability queries (RAG deep-dive)
Use for targeted RAG analysis: KnowledgeGap, Hallucination, RetrievalQuality, AnswerRelevancy, Leaderboard.
runObservabilityQuery()1.3 Reconstruct conversations
Render turn-by-turn timeline from JSON for each session.
ConversationData1.4 Identify issues
Full issue pattern table and classification categories: seereferences/issue-classification.md
Check each session for: action errors, subagent misroutes, missing actions, wrong inputs, variable capture failures, no transitions, slow actions, LOW adherence, abandoned sessions, dead subagents, publish drift, dead hub anti-pattern, entry answering directly, and safety issues.
Priority: P1 = action errors, misroutes, LOW adherence; P2 = missing actions, variable bugs, knowledge gaps; P3 = performance, abandoned sessions.
1.5 Present findings and agent config evidence
Present sessions analyzed, issues grouped by root cause category, and uplift estimate. Then automatically proceed to analyze the file to confirm root causes.
.agentFull structural analysis checks, cross-reference procedures, and publish drift detection: seereferences/issue-classification.md
Retrieve the file from the org, run automated checks (subagent count vs action blocks, dead hub detection, orphan actions, cross-subagent variable dependencies), and cross-reference STDM symptoms against the file structure.
.agentPhase 2: Reproduce -- Live Preview
Full preview procedures, trace diagnosis commands, and classification criteria: seereferences/reproduce-reference.md
Build one test scenario per confirmed issue from Phase 1. Run each through with (generates local traces). Run each scenario 3 times and classify:
sf agent preview--authoring-bundle| Verdict | Criteria |
|---|---|
| Same failure in 3/3 runs |
| Failure in 1-2 of 3 runs |
| Passes in 3/3 runs |
Only and issues proceed to Phase 3.
[CONFIRMED][INTERMITTENT]Key commands:
bash
sf agent preview start --json --authoring-bundle <Name> -o <org>
sf agent preview send --json --session-id "$SID" --utterance "<text>" --authoring-bundle <Name> -o <org>
sf agent preview end --json --session-id "$SID" --authoring-bundle <Name> -o <org>Trace location:
.sfdx/agents/{Name}/sessions/{sessionId}/traces/{planId}.jsonPhase 3: Improve -- Edit .agent File Directly
Full procedures for pre-flight checks, fix mapping, instruction principles, regression prevention, deployment chain, verification, safety re-verification, and test case creation: seereferences/improve-reference.md
3.0 Pre-flight
Verify all action targets exist and are registered in the org before editing. If targets are missing, present options: deploy stubs, remove actions, register via UI, or proceed with routing-only fixes.
3.1-3.3 Map issue, edit, and follow instruction principles
Map each confirmed issue to a fix location in the file (description, instructions, actions, bindings, transitions). Use the Edit tool for targeted changes. Follow instruction principles: name actions explicitly, state pre-conditions, scope tightly, keep persona in only.
.agentsystem:3.4 Regression prevention
Establish baseline before editing. Make minimal edits. Test immediately after each edit. One fix per publish cycle. Check cross-subagent dependencies. Test adjacent subagents.
3.5 Apply fixes
Read the file, edit with the Edit tool (tabs for indentation), show the diff.
.agent3.6 Validate, deploy, publish, activate
bash
# Validate (dry run)
sf agent validate authoring-bundle --json --api-name <AGENT_API_NAME> -o <org>
# Publish (compile + deploy + activate)
sf agent publish authoring-bundle --json --api-name <AGENT_API_NAME> -o <org>If publish fails, use deploy + activate fallback (note: incomplete -- does not propagate to live metadata).
reasoning: actions:3.7 Verify
Run Phase 2 scenarios post-fix. Check trace for correct routing, grounding, tools, and variables. After 24-48 hours, re-run Phase 1 to compare against baseline.
3.7b Safety re-verification (required)
Re-run safety review () on the modified file. Revert any changes that introduce BLOCK findings.
Section 15 of /developing-agentforce.agent3.8 Update Testing Center test cases
Create regression test cases from confirmed issues in Testing Center YAML format. Deploy with and verify all previously-broken scenarios pass.
sf agent test createReference Files
| Reference | Contents |
|---|---|
| STDM query procedures, Apex service deployment, response parsing |
| Issue pattern table, root cause categories, structural analysis checks |
| Phase 2 preview procedures, trace diagnosis, classification criteria |
| Phase 3 editing, deployment chain, verification, safety, test cases |
| DMO field schemas, data hierarchy, quality notes, agent name resolution |