ContextGuides

Your own loop (Anthropic, OpenAI)

Two calls per turn for a hand-written tool-use loop on the Anthropic Messages API or the OpenAI Responses API.

When you call the provider API yourself, withSkills from the anthropic or openai subpath gives you two calls: request(messages) for the next request's fields, and toolResults(messages) to run the pending tool calls.

skills = context.load_skills("../quickstart/skills", tools=[lookup_order, issue_refund, search_docs])
ctx = with_skills(skills)  # expose "deferred": the tools array and the cached prefix never change

messages: list[Any] = [{"role": "user", "content": "My order A-1001 arrived damaged."}]
while True:
    res = await client.create(model="claude-sonnet-5", max_tokens=1024, **await ctx.request(messages))
    messages.append({"role": "assistant", "content": res["content"]})
    if res["stop_reason"] != "tool_use":
        break
    messages.append({"role": "user", "content": await ctx.tool_results(messages)})
const skills = await loadSkills("../quickstart/skills", { tools: [lookupOrder, issueRefund, searchDocs] });
const ctx = withSkills(skills); // expose "deferred": the tools array and the cached prefix never change

const messages: unknown[] = [{ role: "user", content: "My order A-1001 arrived damaged." }];
for (;;) {
  const res = await fakeClient.create({ model: "claude-sonnet-5", max_tokens: 1024, ...(await ctx.request(messages)) });
  messages.push({ role: "assistant", content: res.content });
  if (res.stop_reason !== "tool_use") break;
  messages.push({ role: "user", content: await ctx.toolResults(messages) });
}
  • The session is rebuilt from your messages on every call: the transcript is the state.
  • toolResults runs every call without a result yet, in order, on a session rebuilt from the messages before them, so a load_skill among them applies live and emits its event. Unbound tools are refused, never run.
  • Router and trigger loads are not in the transcript. TypeScript remembers them for the same conversation array; in Python (lists cannot be weakly referenced), save (await ctx.session(messages)).state and pass it as state= on the next call. Passing state works in both.

Anthropic. The default exposure is deferred: every bindable tool is declared with defer_loading, and a load_skill result carries tool_reference blocks for the tools it binds. The tools array never changes, so loading a skill never invalidates the cached prefix. Cache markers go on the static and session system blocks.

OpenAI. The Responses API by default (api: "chat" for Chat Completions). expose: "allowed" declares every bindable tool in a fixed order and restricts with tool_choice allowed_tools, which keeps the prefix cached at the cost of sending every schema; the default, bound, sends only bound tools.