Skip to content

Add offline mock client to replace Anthropic API dependency#115

Open
gyakusu wants to merge 25 commits intoshareAI-lab:mainfrom
gyakusu:claude/standardize-env-setup-M52Bd
Open

Add offline mock client to replace Anthropic API dependency#115
gyakusu wants to merge 25 commits intoshareAI-lab:mainfrom
gyakusu:claude/standardize-env-setup-M52Bd

Conversation

@gyakusu
Copy link

@gyakusu gyakusu commented Mar 22, 2026

Summary

This PR introduces a new MockAnthropic client that provides an offline, pattern-based mock implementation of the Anthropic API. All agent scripts now use this mock client instead of requiring actual API credentials and network access.

Key Changes

  • New agents/mock_client.py: Implements MockAnthropic class with MockMessages that mimics client.messages.create() behavior

    • Pattern-based response generation using regex matching on user input
    • Supports both lead agent behavior (spawning teammates, approvals, shutdowns) and teammate behavior (plan submission, shutdown responses)
    • Generates realistic tool_use and text responses with proper dataclass structures
    • Helper methods for extracting context from message history
  • Updated all agent scripts (s01 through s12, s_full.py):

    • Replaced from anthropic import Anthropic with from agents.mock_client import MockAnthropic
    • Removed load_dotenv() and environment variable setup for API keys
    • Changed client = Anthropic(...) to client = MockAnthropic()
    • Changed MODEL = os.environ["MODEL_ID"] to MODEL = "mock"
  • Code cleanup across agent scripts:

    • Renamed private functions to public (e.g., _run_bashrun_bash, _safe_pathsafe_path)
    • Improved variable naming (e.g., ccontent)
    • Standardized output messages (e.g., "Wrote X bytes" instead of "Wrote X bytes to path")
    • Enhanced bash command safety checks (added "> /dev/" to dangerous commands list)
  • Updated .gitignore: Added .team/ directory to ignored paths

  • Updated requirements.txt: Removed anthropic>=0.25.0 dependency

Implementation Details

The mock client uses keyword pattern matching to simulate agent behavior:

  • Lead agent: Recognizes spawn, shutdown, list, approval, rejection, and inbox commands
  • Teammate agent: Recognizes shutdown acknowledgments, plan submissions, and approval responses
  • Generates unique tool IDs using UUID for realistic tool_use blocks
  • Extracts context from message history to maintain conversation state
  • All responses are deterministic and require no external API calls

This enables running and testing the entire agent system offline without API credentials.

https://claude.ai/code/session_01Afh2tutefmEuAEAP4ruGz5

claude and others added 24 commits March 22, 2026 05:36
Create mock_client.py that simulates Anthropic Messages API responses
so learners can run `MOCK=1 python agents/s01_agent_loop.py` without
an API key or network access. Minimal changes to s01: conditional
import based on MOCK env var.

https://claude.ai/code/session_01GabR5FftxYc2PcfEuUmWz1
- tests/mock_api.py: stdlib http.server mock for Anthropic Messages API
  with scripted scenarios matching the s02 demo prompts
- tests/run_s02_offline.sh: one-command launcher for offline use
- docs/ja/s02-tool-use.md: add offline usage instructions

https://claude.ai/code/session_01MkuZcHUYg2cadrXTQj5bEk
- Create tests/mock_client.py with scripted Anthropic API responses
  that demonstrate TodoManager + nag reminder behaviour
- Modify agents/s03_todo_write.py to use MockAnthropic when MOCK=1
- Add tests/test_s03.py with 13 tests covering TodoManager validation,
  nag reminder logic, and mock scenario integration

Run: MOCK=1 python agents/s03_todo_write.py
Test: python -m pytest tests/test_s03.py -v

https://claude.ai/code/session_018fZRQdSaMZfydDJP16BY1g
Add mock_s05.py that simulates the Anthropic API responses for the
two-layer skill injection pattern. Set OFFLINE=1 to run without an
API key. Minimal change to s05_skill_loading.py to switch clients.

https://claude.ai/code/session_01HuvjsR5RS8DszGQd2Uokv2
Introduce agents/mock_client.py with scripted Anthropic SDK responses
so the s06 tutorial runs without an API key (MOCK=1). The mock replays
a sequence of read_file tool calls to demonstrate all three compression
layers (micro_compact, auto_compact, manual compact).

s06_context_compact.py: swap in MockAnthropicClient when MOCK env var
is set; soften MODEL_ID to os.getenv with a default.

https://claude.ai/code/session_01N84zDvpdNowKwZETvDk8Z7
Enable s08_background_tasks.py to run without an API key by
auto-detecting missing credentials (or MOCK=1) and falling back
to a pattern-matching mock client that simulates tool_use responses
for background task scenarios.

https://claude.ai/code/session_01DvgAJyhDY2dgNZSywpH6rT
- tests/mock_server.py: lightweight HTTP server that mimics /v1/messages,
  returning scripted tool_use responses based on conversation context
- agents/s09_agent_teams.py: auto-set dummy ANTHROPIC_API_KEY when
  ANTHROPIC_BASE_URL is configured (1-line change)
- docs/ja/s09-agent-teams.md: add offline usage instructions

https://claude.ai/code/session_018WrEioNtZjL1wSxRNrGPXk
- Create agents/mock_server.py: standalone HTTP mock that simulates the
  Anthropic Messages API with scripted lead/teammate responses
- Add --mock flag to s11_autonomous_agents.py for one-command offline use
- Mock demonstrates task creation, spawning, auto-claiming, and idle cycles

https://claude.ai/code/session_01Mv27P68bcRX7Cj6N8EeA8q
Create mock_s12.py with scripted responses matching the 5-step tutorial
flow, and add USE_MOCK=1 flag to s12 for running without API access.

https://claude.ai/code/session_01FdznH79o3hX2ktaNwTgAmB
Add offline mock for s12 worktree tutorial
Add offline mock client for s01 agent loop tutorial
add offline mock API server for s02 tutorial
feat(s05): add offline mock for skill-loading tutorial
- Create agents/mock_client.py: drop-in MockAnthropic replacement with
  scripted scenarios (test framework detection, file summarization)
- Modify s04_subagent.py to use mock when MOCK=1 env var is set
- Run with: MOCK=1 python agents/s04_subagent.py

https://claude.ai/code/session_015jMzuncxKNMKW9p61jted4
Add mock Anthropic client for offline s08 tutorial
Add offline mock client for s04 subagent tutorial
Add mock Anthropic API server for offline s09 testing
Merge the s06 context-compact scripted scenario into the existing
mock_client.py from the study branch. The unified file supports:
- s01-s03: generic pattern-based responses
- s04: scenario-based parent/child subagent responses
- s06: scripted read_file sequence with summarisation for auto_compact

Add MockAnthropicClient alias so both import styles work.

https://claude.ai/code/session_01N84zDvpdNowKwZETvDk8Z7
- Add s07 task tool scenarios to mock_client.py: supports create-N-tasks
  with dependency wiring, task board with parallel deps, complete+list,
  and list tasks patterns.
- Update s07_task_system.py to use MOCK=1 pattern consistent with other
  tutorials (s04, s06).

Usage: MOCK=1 python agents/s07_task_system.py

https://claude.ai/code/session_0187J4kvcgLJc9DWPWYoJvvS
Add offline mock support for s07 task system tutorial
@vercel
Copy link

vercel bot commented Mar 22, 2026

@claude is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

- Replace all Anthropic API imports with `from agents.mock_client import MockAnthropic`
- Remove env-var toggling, sys.path hacks, and per-session mock files
- Standardize variable naming: `c` → `content` in run_edit, `_safe_path` → `safe_path`, `_run_*` → `run_*`
- Standardize output strings: consistent "bytes" and "more" messages
- Add `> /dev/` to dangerous commands blocklist in s09/s10/s11
- Delete redundant mock_s05.py, mock_s12.py, mock_server.py
- Remove anthropic from requirements.txt

https://claude.ai/code/session_01Afh2tutefmEuAEAP4ruGz5
@gyakusu gyakusu force-pushed the claude/standardize-env-setup-M52Bd branch from b7aa85c to 215c4cb Compare March 23, 2026 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants