Skip to content

CCDV-F · Domain 1: Agents and Workflows · 17% of the exam

Task 1.2: Agent SDK Construction and Hooks

Writing and fixing Claude Agent SDK agent definitions, custom loops and harnesses, and hooks, so that the steps that must happen run in code every time instead of when the model decides to.

Study it

  • Agent SDK agents, custom loops and hooks that make required steps run every time

    Partly covered by: Guardrails

Sample questions

Try each one before opening the answer. Every option is explained, with the Anthropic documentation page that proves it.

Question 1 · choose 1

Your Python agent passes env={"API_TIMEOUT_MS": "120000"} in ClaudeAgentOptions and runs in a container whose environment sets PATH and the other variables the agent relies on. You port it to the TypeScript Agent SDK and run it in the same container with this option:const result = query({ prompt: task, options: { env: { API_TIMEOUT_MS: "120000" } }, });What should you change so that the TypeScript agent gets the same environment as the Python one?

  1. ANothing, because both SDKs merge env on top of the environment the process inherited
  2. BPut API_TIMEOUT_MS in a .env file next to the entry point, which the SDK reads at startup
  3. CBuild env from process.env and add API_TIMEOUT_MS to it, so that the inherited variables are kept
  4. DMove API_TIMEOUT_MS into a settings file, since the TypeScript SDK reads timeout variables only from settings
Show the answer and why
  • ANothing, because both SDKs merge env on top of the environment the process inherited

    Incorrect

    Only the Python SDK merges env; in TypeScript a set env replaces the subprocess environment.

  • BPut API_TIMEOUT_MS in a .env file next to the entry point, which the SDK reads at startup

    Incorrect

    The SDK doesn't load .env files automatically, and the env option would still replace the inherited variables.

  • CBuild env from process.env and add API_TIMEOUT_MS to it, so that the inherited variables are kept

    Correct

    A set env replaces the subprocess environment in TypeScript, so spread process.env into it to keep variables such as PATH.

  • DMove API_TIMEOUT_MS into a settings file, since the TypeScript SDK reads timeout variables only from settings

    Incorrect

    The reference passes the timeout variables through the env option, which the CLI subprocess reads.

In the TypeScript Agent SDK, setting env replaces the CLI subprocess's environment instead of merging with process.env, so pass { ...process.env, API_TIMEOUT_MS: "120000" } to keep inherited variables such as PATH. The Python SDK's env is merged on top of the inherited environment.

Question 2 · choose 1

Your own agent loop on the Messages API runs a take_screenshot tool that returns PNG bytes. The loop answers each call like this:png_b64 = base64.b64encode(png_bytes).decode() results.append({ "type": "tool_result", "tool_use_id": block.id, "content": png_b64, })Claude then describes the result as a long encoded string and never comments on what the screen shows. What is the fix?

  1. ASend the screenshot as a separate user message placed between the tool use and the tool result
  2. BMake content a list holding an image block whose base64 source carries the PNG and its media type
  3. CSet is_error to true on the result so that Claude inspects its content more closely
  4. DReturn the bytes in the next request's system prompt, keeping the tool_result empty
Show the answer and why
  • ASend the screenshot as a separate user message placed between the tool use and the tool result

    Incorrect

    Tool results must immediately follow their tool use; nothing may sit between the two messages.

  • BMake content a list holding an image block whose base64 source carries the PNG and its media type

    Correct

    A tool_result's content can be a list of blocks, including image blocks, which Claude reads as images.

  • CSet is_error to true on the result so that Claude inspects its content more closely

    Incorrect

    is_error marks a failed call; it does not change how the content is read.

  • DReturn the bytes in the next request's system prompt, keeping the tool_result empty

    Incorrect

    The system prompt is text; the result belongs in the tool_result that answers the call.

A tool_result's content is either a string or a list of content blocks of the text, image, document or search_result types. A string is read as text, so a screenshot should go in an image block inside the content list.

Question 3 · choose 2

Your Python Claude Agent SDK app needs two behaviors that must happen every time, not when Claude decides to: the run must end for good as soon as any call to the quota_check tool returns "QUOTA_EXHAUSTED", and every prompt the agent receives, including turns the agent starts on its own, must get the tenant's policy note added as extra context. Which TWO hook configurations provide these? (Choose TWO.)

  1. AA SessionStart hook that loads the policy note once when the session initializes
  2. BA PostToolUse hook with the matcher "QUOTA_EXHAUSTED" that runs when a result contains that text
  3. CA PostToolUse hook on quota_check that returns continue set to false when the result reports exhaustion
  4. DA UserPromptSubmit hook that injects the policy note as additional context
  5. EA PreToolUse hook on quota_check that returns permissionDecision "ask" before every call
Show the answer and why
  • AA SessionStart hook that loads the policy note once when the session initializes

    Incorrect

    SessionStart is available only in the TypeScript SDK, not in Python.

  • BA PostToolUse hook with the matcher "QUOTA_EXHAUSTED" that runs when a result contains that text

    Incorrect

    For tool events the matcher is tested against the tool name, not against the result's text.

  • CA PostToolUse hook on quota_check that returns continue set to false when the result reports exhaustion

    Correct

    The top-level continue field decides whether the agent keeps running after the hook.

  • DA UserPromptSubmit hook that injects the policy note as additional context

    Correct

    It fires whenever a prompt is submitted, including turns Claude Code starts on its own.

  • EA PreToolUse hook on quota_check that returns permissionDecision "ask" before every call

    Incorrect

    Asking before each call neither reads the result nor ends the run.

Hooks run your code on agent events. A PostToolUse hook sees each result and can return continue false to stop the agent; a UserPromptSubmit hook fires on every submitted prompt and can inject context. Matchers for tool events filter by tool name, and SessionStart is a TypeScript-only event.

Question 4 · choose 1

A custom tool in your Agent SDK app is marked with readOnlyHint: true, but a code review finds that its handler also writes a cache file to disk. What does the SDK documentation say about this annotation?

  1. AThe SDK blocks any disk write from a tool marked read-only and returns an error to Claude
  2. BThe annotation is ignored unless the tool also sets destructiveHint to false
  3. CAnnotations are metadata, not enforcement, so keep the hint accurate to the handler
  4. DThe write is rolled back after the call, because read-only tools run against a copy of the filesystem
Show the answer and why
  • AThe SDK blocks any disk write from a tool marked read-only and returns an error to Claude

    Incorrect

    Nothing checks the handler; annotations are not enforced.

  • BThe annotation is ignored unless the tool also sets destructiveHint to false

    Incorrect

    Each hint is read on its own; readOnlyHint lets Claude batch the tool with other read-only calls.

  • CAnnotations are metadata, not enforcement, so keep the hint accurate to the handler

    Correct

    A tool marked readOnlyHint can still write to disk if its handler does; keep the annotation accurate.

  • DThe write is rolled back after the call, because read-only tools run against a copy of the filesystem

    Incorrect

    No copy or rollback is made; the handler runs as written.

Tool annotations are optional metadata describing how a tool behaves. They are not enforcement: a tool marked readOnlyHint: true can still write to disk if that is what its handler does, so keep each annotation accurate to the handler.

Question 5 · choose 1

Your TypeScript Claude Agent SDK service must send an alert every time a turn ends with an API error instead of a normal stop. Which hook event should you register?

  1. AStopFailure
  2. BStop
  3. CPostToolUseFailure
  4. DSubagentStop
Show the answer and why
  • AStopFailure

    Correct

    StopFailure fires when the turn ends with an API error instead of a normal stop.

  • BStop

    Incorrect

    Stop fires when agent execution stops normally.

  • CPostToolUseFailure

    Incorrect

    That event covers a failed tool execution, not a turn that ends on an API error.

  • DSubagentStop

    Incorrect

    SubagentStop fires when a subagent completes.

The SDK's hook events include Stop for a normal end of execution and, in TypeScript, StopFailure for a turn that ends with an API error instead of a normal stop, which suits logging failures or sending alerts.

Practice domain 1 →Practice all domains →