In the world of AI, we love buzzwords. You’ve probably heard of “prompt engineering” and “Retrieval Augmented Generation (RAG).” But as we build more sophisticated AI agents, a new, more important concept is taking center stage: context engineering.
If you’re working with Agentforce, mastering context engineering is the key to building agents that are truly effective and reliable. It’s about moving beyond a single prompt and instead architecting the agent’s entire operational memory. This means strategically managing all the information an agent uses to make decisions: its core goal, the tools it can use, the history of its actions, and the data it observes. In this post, we’ll break down these core principles and show you how to apply them directly within Agentforce to build reliable agents.
What is context engineering?
Context engineering is the art and science of giving your AI agent the right information, the right tools, and the right instructions, so that the agent is able to accomplish the given goal.
You might be wondering, “Isn’t that just prompt engineering?” Not quite. Prompt engineering is a part of context engineering, but it’s only one piece of the puzzle.
Context engineering is the whole picture. It includes:
- The prompt: Your direct instructions to the agent.
- Tools & actions: The actions your agent can take, like running flows, calling Apex, or using prompt templates. Tools can also be MCP Servers
- Knowledge base (RAG): The information your agent can look up from Data Cloud, knowledge articles, Slack, or other data sources.
- Memory: Both short-term memory of the current conversation and long-term memory of past interactions.
- Guardrails: The rules that keep your agent safe and focused on the task.
Getting context engineering right helps you avoid common problems that can make an AI agent fail, such as:
- Context distraction: Giving the agent too many irrelevant tools
- Context clash: Providing conflicting instructions that confuse the agent
- Context poisoning: Letting bad or outdated information into the system
- Context confusion: Overwhelming the agent with too many complex tasks at once
How to streamline context with Agentforce
Agentforce is designed to let developers engineer an agent’s context from the ground up. Here’s how the platform provides all the necessary components.
Organizing context with “topics and actions”
This is where Agentforce shines. Instead of giving your agent a long list of tools, Agentforce organizes them in a simple way: topics. A topic is a container for a specific job-to-be-done, like “Order Management” or “Lead Qualification.” Inside each topic, you place all the actions that an agent needs to handle that job. In Agentforce, your “tools” are actions. These actions can be created from flows, Apex methods, APIs, or even other prompt templates.
This structure is a powerful form of context engineering because it allows the agent to narrow its focus, especially once you’ve optimized your topics and actions. When a user asks a question, the agent first determines the correct topic. Once inside that topic, it only has to choose from a small, relevant set of actions, making it faster and more accurate.
Managing memory
Long conversations can overwhelm an agent because it has to track every turn in the conversation. Agentforce helps manage this by using variables that hold key information from the current conversation. You can create actions that summarize the chat as it goes. This keeps the immediate context clear and concise, helping the agent stay on track without hitting context window limits.
Retrieving dynamic knowledge (RAG)
Your agent can’t know everything at once. That’s where RAG comes in. Agentforce connects seamlessly with Data Cloud to fetch information on the fly. When a user asks a question, the agent can perform a powerful vector search across your knowledge articles, PDFs, and CRM data. It pulls in only the relevant snippets, giving the agent the exact fact it needs to answer the question without cluttering its context.
Mastering the prompt
Prompts are instructions given to the agent to accomplish specific goals. Salesforce makes this easy with tools like Prompt Builder and other natural language instructions you provide via Agent Builder. Prompts are still a critical piece of the context engineering landscape and you should make sure to frequently test and refine your prompts.
Specifying agent guardrails
Guardrails are the rules and boundaries you give your agent to stay on track. In Agentforce, this includes topic instructions that guide behavior and reasoning, and scopes that define what the agent is allowed to do and not do. Instructions and scopes are written in natural language. Additionally, Agentforce allows you to create filters for topics and actions. This ensures that your agent can use them in a conversation only when certain conditions are met. Instruction Adherence is another type of guardrail that lets you monitor how well your AI agents follow topic instructions.
Context engineering best practices with Agentforce
Building a reliable agent requires a thoughtful approach. Here are key context engineering best practices to follow when building with Agentforce:
- Define clear objectives: Before you start feeding information to your agent, articulate precisely what you want your Agentforce agent to achieve. For instance, “What specific customer service inquiries should this agent handle effectively?” or “Which sales workflows should this Agentforce solution automate?” This clarity is fundamental for identifying and curating truly relevant context.
- Curate information rigorously: Be ruthless in your selection of information. Include only what is directly pertinent to the task. Avoid adding extraneous Salesforce data (e.g., irrelevant fields from objects, or an outdated knowledge article) that could dilute your context.
- Curate and limit your topics and actions: The most direct way to prevent “context confusion” is to be selective. Limit the number of topics (10 max) that an agent has access to, and keep the number of actions within each topic low (12-15 max).
- Control probabilistic behavior: AI agents are inherently probabilistic, meaning they can give different responses to the same input. To increase deterministic behavior in your Agentforce solution, use structured actions (flows, Apex methods) for critical business logic rather than relying solely on natural language instructions. Also, define variables and conditional logic within your actions to ensure consistent outcomes for key processes. Reserve AI’s probabilistic nature for areas where creativity and flexibility add value, like generating personalized responses or interpreting natural language queries.
- Ensure consistency: Scrutinize all elements of your context — prompts, topics, actions, RAG data, internal instructions, and Salesforce configuration — for any contradictions. Eliminating conflicting information prevents “context clash” within your Agentforce solution.
- Validate and ground data: For RAG that leverages Agentforce Data Library, implement robust data validation processes. Ensure that your knowledge articles, uploaded files, CRM data, and Data Cloud inputs are as accurate and as up-to-date as possible. Pay attention to how your content is chunked and vectorized in Data Cloud’s search indexes as poor chunking can lead to irrelevant or incomplete context retrieval. Consider using hybrid search (vector + keyword) for better retrieval accuracy when appropriate.
- Structure and prioritize: Organize your context logically. If your Agentforce solution uses multiple solutions (e.g., topics, actions, prompt templates), clearly define their purpose and prioritize their usage to avoid context confusion for your agent. Actively look for any conflicting instructions, data points from different Salesforce objects, or tool functionalities within your entire context. These contradictions can cause unpredictable Agentforce behavior.
- Iterate and refine: Context engineering is an ongoing process. Test your agents via Agentforce Testing Center and continuously monitor your Agentforce solution’s performance in Command Center. Gather user and agent feedback (e.g., from conversation transcripts). Use these insights to refine and optimize your context based on user and agent feedback
Conclusion
Ultimately, building a powerful agent in Agentforce comes down to one core principle: context gives you the full picture. While a great prompt is important, it’s the carefully engineered ecosystem around it — curated actions, clean data from RAG, clear instructions, and clear guardrails — that separates a flashy demo from a reliable business solution. The best practices outlined here are your roadmap to success. Start small, define your agent’s objectives clearly, and iterate relentlessly.
References
About the authors
Mohith Shrivastava is a Principal Developer Advocate at Salesforce with 14 years of experience building enterprise-scale products on the Salesforce Platform. Mohith is currently among the lead contributors on Salesforce Stack Exchange, a developer forum where Salesforce Developers can ask questions and share knowledge. You can follow him on LinkedIn.
Yuvi Zalkow is a Principal Technical Writer at Salesforce with 20 years of experience writing developer docs and previous career experience as a software developer.
The post A Developer’s Guide to Context Engineering with Agentforce appeared first on Salesforce Developers Blog.