Implement Claude tool use

SupportedPython 3.12+Anthropic Messages APILearning series · Part 2MODEL_CALL + TOOL_CALL

Define a Messages API tool schema, execute the selected tool in application code, return a tool result, and distinguish a failed lookup from a failed model run.

Last updated

1. Learning objective

Define a Claude tool

Define a Messages API tool schema, execute the selected tool in application code, return a tool result, and distinguish a failed lookup from a failed model run.

  • Guide 1 environment
  • The direct Claude API basics lab

2. Technical context

Understand tool execution

An insurance assistant must look up a policy before answering. Claude can select the tool, but only the application can decide where and how that lookup executes.

3. Run without AIGP

Run the tool example

Run first_tool.py. It exposes exactly one local lookup_policy function backed by a deterministic fixture and performs the documented Messages API tool-result round trip.

terminal · lab
cd labs/agent-frameworks/anthropic/claude-api-basics-intg
source .venv/bin/activate
python first_tool.py
# Failure experiment:
python first_tool.py POL-404

4. Configure AIGP

Configure tool tracking

Reuse the optional Agent Executions configuration from Guide 1. The adapter records MODEL_CALL and TOOL_CALL boundaries with a hashed native tool-use ID, status, reason code, and latency only.

Privacy boundary: retain bounded identifiers, lifecycle, status, timing, digests, and references only. Do not submit prompts, responses, tool arguments/results, credentials, or raw SDK payloads.

Authenticate local AIGP requests

Obtain a short-lived token

If your local OSS stack uses Keycloak, the organization and project headers are scope selectors, not authentication. Generate a token from the generated local OAuth client and pass it only through AI_GOVERNANCE_ACCESS_TOKEN for this terminal session.

terminal · local Keycloak token
# Run from the OSS repository after its local Keycloak stack is ready.
cd /path/to/ai-governance-oss
./servers.sh up

# This ignored file contains the local walkthrough client credentials.
# It does not contain an access token.
set -a
source .env.oauth.generated
set +a

# Mint one short-lived token in memory for this terminal only.
export AI_GOVERNANCE_ACCESS_TOKEN="$(
  uv run python -c 'from ai_governance.oauth import access_token_from_environment; print(access_token_from_environment())'
)"

# Return to the Claude lab. Its .env supplies Claude and AIGP scope settings.
cd /path/to/labs/agent-frameworks/anthropic/claude-api-basics-intg
set -a
source .env
set +a
python first_tool.py

# Remove the in-memory token when the session is over.
unset AI_GOVERNANCE_ACCESS_TOKEN

Never commit, print, paste into a screenshot, or add the token to .env. For development-mode and production workload identities, see Developer Authentication.

5. Inspect execution evidence

Inspect tool-call evidence

Inspect the returned execution timeline. A successful lookup produces two MODEL_CALL records and one TOOL_CALL. A POLICY_NOT_FOUND result is a failed tool outcome, not proof that the application successfully looked up a policy.

Only use a Studio screenshot after a run has been completed and its route, tenant scope, and visible data have been verified. This guide does not substitute a conceptual image for product evidence.

Verified Studio record for the local tool-use example. The evidence shows the reported lifecycle without exposing tool inputs or results.

6. Test failure handling

Test missing policies

Run the POL-404 experiment. The local tool returns an explicit error result to Claude. Do not replace it with an invented success or an AIGP decision.

7. Technical boundaries

Understand tool boundaries

  • Claude chooses a tool from the schema.
  • The application validates and executes the tool, then returns its result.
  • AIGP observes the execution boundary; it does not select, execute, or repair the tool call.

8. Next step

Connect MCP

This is a documented integration boundary, not an implicit AIGP feature. Continue only with the capabilities explicitly marked ready in the learning path.

Back to developer guides