Skip to content

Instructions

Instructions are the scaffolding that steer every run. When you build an agent you can pass plain strings, context-aware functions, or async callbacks; right before each session starts the library resolves them (in order) into a single system prompt. Because the resolver receives the run context, you can tailor tone, policy, or knowledge base links per tenant without rebuilding the agent.

Keep instructions concise and layered: use static lines for global policy, then dynamic functions for request-specific details. If you need to defer heavy lookups (for example, loading account limits), perform that work inside the instruction function so the result is cached for the duration of the session.

types.ts
type InstructionParam<TContext> =
| string
| ((ctx: TContext) => string)
| ((ctx: TContext) => Promise<string>);

Below is a small agent that mixes static and dynamic instructions across the three SDKs.

instructions.ts
import { Agent, getResponseText } from "@hoangvvo/llm-agent";
import { getModel } from "./get-model.ts";
interface DungeonRunContext {
dungeonMaster: string;
partyName: string;
currentQuest: string;
highlightPlayerClass: string;
getOracleWhisper(): Promise<string>;
}
const model = getModel("openai", "gpt-4o");
const dungeonCoach = new Agent<DungeonRunContext>({
name: "Torch",
instructions: [
"You are Torch, a supportive guide who keeps tabletop role-playing sessions moving. Offer concrete options instead of long monologues.",
(context) =>
`You are helping ${context.dungeonMaster}, the Dungeon Master for the ${context.partyName}. They are running the quest "${context.currentQuest}" and need a quick nudge that favors the party's ${context.highlightPlayerClass}.`,
async (context) => {
const whisper = await context.getOracleWhisper();
return `Weave in the oracle whisper: "${whisper}" so it feels like an in-world hint.`;
},
],
model,
});
const context: DungeonRunContext = {
dungeonMaster: "Rowan",
partyName: "Lanternbearers",
currentQuest: "Echoes of the Sunken Keep",
highlightPlayerClass: "ranger",
async getOracleWhisper() {
await new Promise((resolve) => setTimeout(resolve, 25));
return "the moss remembers every secret step";
},
};
const response = await dungeonCoach.run({
context,
input: [
{
type: "message",
role: "user",
content: [
{
type: "text",
text: "The party is stuck at a collapsed bridge. What should happen next?",
},
],
},
],
});
console.log(getResponseText(response));