Skip to content
mentoor

Topic 4 of 5 · 4 min read

Building on agent SDKs programmatically

Goal: Choose between a CLI call and an agent SDK for an automation and outline the session, tools and permission checks the code needs.

Use the CLI when you need one prompt and one answer; use an agent SDK when your own code must hold a session, add its own tools or decide each permission at run time.

Check yourself · 1 of 3

When is an agent SDK a better fit than a one-shot CLI call?

Choose one answer

Why it matters

A shell call is simple, and for many jobs it is enough: "summarise this diff", "review this file". But some automations need more. A support tool may keep a conversation going across several messages. A product may let the agent call your internal API. A service may need to approve or deny each action based on rules only your code knows. The Claude Agent SDK, the Codex SDK and similar libraries expose the same agent loop as the CLI, but as functions you call from TypeScript or Python.

How to choose

Need Choice
One prompt, one result, in a script or CI CLI call (claude -p, codex exec)
Several turns that share context SDK session, or CLI with a resume option
Your own tools, such as "look up order" SDK custom tool
Per-action approval by code SDK permission callback
Streaming progress to a user interface SDK event stream

What the code needs

  1. A session. Create it, keep its id, and resume it rather than resending history. Decide when it ends and how long it may live.
  2. Tools. Start with the built-in file tools restricted to a working directory. Add custom tools with a clear name, a description the model can follow and a typed input. Validate inputs inside the tool, because the model fills them in.
  3. A permission check. The Claude Agent SDK lets you pass a callback (canUseTool) that sees each tool call and returns allow or deny. Write the rule in code: allow reads, allow edits under src/, deny everything else, and log each decision.
  4. Limits and errors. Set a turn cap and a timeout, and handle the error and cancelled outcomes explicitly.
// Sketch: the shape of a permission callback, not a full program
async function canUseTool(toolName: string, input: Record<string, unknown>) {
  if (toolName === "Read") return { allow: true };
  if (toolName === "Edit" && String(input.file_path).startsWith("/work/src/")) {
    return { allow: true };
  }
  audit.log({ toolName, input, decision: "deny" });
  return { allow: false, reason: "Only src/ may be edited" };
}

Before and after

Weak: a service shells out to claude -p with a user's text pasted into the command line, once per message, with no memory between calls and no control over the tools.

Better: the service opens one SDK session per conversation, registers a single custom lookup_order tool that validates its input, and uses a permission callback that denies anything outside that tool and a read-only file set.

What goes wrong

  • Over-building. A one-shot job gains nothing from an SDK and adds code to maintain.
  • Trusting tool inputs. The model chooses the arguments. Validate them as you would user input.
  • No permission rule. A permissive default lets a service act with more power than the feature needs.
  • Sessions that never end. Context grows, cost grows and old instructions linger.

Examples

A support assistant

A help desk tool keeps one session per ticket and registers a lookup_order tool that checks the order id format before calling the internal API. The permission callback denies shell and file writes, so the agent can read an order and nothing else.

When the CLI is enough

A weekly job asks the agent to summarise merged pull requests into a changelog. It is one prompt with a schema and one result, so a single claude -p call in a scheduled workflow is simpler than an SDK service.

Key points

  • Use the CLI for one prompt and one answer, and an SDK when your code needs sessions, custom tools or per-action decisions.
  • An SDK exposes the same agent loop as the CLI as functions in your own language.
  • Custom tools need clear descriptions and input validation, because the model supplies the arguments.
  • A permission callback lets code allow or deny each action, and it should log every decision.
  • Set a turn cap, a timeout and an end of life for every session.

Common mistakes

  • Building an SDK service for a job a single CLI call would do.
  • Registering custom tools without validating their inputs.
  • Leaving the permission policy at a permissive default in a service.
  • Reusing one long session for unrelated users or tasks.

Check yourself · 2 of 3

What does a permission callback in an agent SDK let your code do?

Choose one answer

Check yourself · 3 of 3

You add a custom tool for an agent. What should the tool do with the arguments it receives?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic