Claude Code Restored Source Tree

repository·main·Indexed 25 days ago

https://github.com/oboard/claude-code-rev

A reconstructed source tree of the Claude Code CLI (version 999.0.0-restored) built from source maps. It provides a runnable workspace for developers and includes documentation for the Python and TypeScript Agent SDKs, as well as the standard Anthropic SDKs for interacting with Claude models via the Messages API.

Tokens
27.2K
Snippets
44
Records
174
Agent score
82%

What's inside claude-code-rev

  1. Implement simple text streaming in Python

    main

    Use the client.messages.stream context manager to receive text incrementally as it is generated. Iterate over stream.text_stream to access the incoming text chunks. This is ideal for chat interfaces or terminal UIs where you want to provide immediate feedback to the user.

    from anthropic import Anthropic
    
    client = Anthropic()
    
    with client.messages.stream(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Write a short release note."}],
    ) as stream:
        for text in stream.text_stream:
            print(text, end="", flush=True)
  2. Implement robust Anthropic API error handling

    main

    Follow these best practices when implementing error handling for Anthropic API integrations:

    • Correlation: Always log the Anthropic request ID when available to allow for correlation of failed requests.
    • Logging: Surface response body details in server logs for debugging, but ensure full prompts or secrets are not leaked to end-users.
    • Idempotency: Ensure retries are idempotent, particularly for batch polling and file-based workflows.
    • Error Distinction: Explicitly distinguish between validation failures (request-specific) and transport failures (network/service-specific) in your logic paths.
  3. Verify CLI changes using the Verify CLI pattern

    main

    When a change affects a CLI command, flag, formatter, or local workflow, use this verification pattern to ensure correctness:

    1. Run the narrowest command that hits the specific changed code path.
    2. Test nearby flags if the change involved argument parsing or help text.
    3. Validate both output and exit behavior (ensure the command exits with the expected status code and produces the expected text).

    This pattern is particularly useful for verifying that a restored entrypoint reaches the real CLI path rather than a stub.

    bun run version
    bun run dev --help
  4. Narrow tool access in the TypeScript Agent SDK

    main

    To improve security and reliability, follow the principle of least privilege by providing the smallest possible tool surface required for a task. Avoid giving write-capable tools unless the task explicitly requires edits.

    const agent = new Agent({
      model: 'claude-sonnet-4-6',
      systemPrompt: 'Review code and report findings only.',
      allowedTools: ['Read', 'Glob', 'Grep'],
    })
  5. Use the Verify skill to validate code changes

    main

    The Verify skill is used when a task requires exercising a code change to ensure it works as intended. The goal is to produce a short verification result grounded in actual execution rather than inference.

    Workflow

    1. Identify the changed surface area.
    2. Pick the smallest realistic verification path.
    3. Run the relevant command or request flow.
    4. Capture the observable result (e.g., exit status, key output, HTTP status, or changed behavior).
    5. Report what passed, what was not verified, and any remaining risk.

    Rules for Verification

    • Do not claim success without running an actual command or process.
    • Prefer focused, narrow checks over broad smoke tests.
    • If no formal test target exists, use the nearest runnable workflow.
    • If environment limits block a check, state this explicitly.
    • Include exact commands used to allow for repeatable verification.
  6. Best practices for implementing Claude tools

    main

    Follow these guidelines when building tools for Claude:

    Do:

    • Keep tool schemas narrow and explicit.
    • Validate tool input before execution.
    • Return structured, minimal results instead of raw logs.
    • Handle authorization and side-effect checks within your application code rather than relying on the model prompt.

    Avoid:

    • Exposing shell or network primitives unless absolutely necessary.
    • Using vague tool names or overly broad schemas.
    • Skipping retries and timeout handling for real-world integrations.
  7. Best practices for using the Anthropic Python SDK

    main

    When building with the Anthropic Python SDK, follow these practical guidelines:

    • Model Selection: Prefer current stable model aliases or exact IDs supported by your application.
    • Prompt Caching: Keep long-lived static context at the start of the request to leverage prompt caching.
    • Latency: Use streaming for long outputs or latency-sensitive user interfaces.
    • Large Jobs: Use the Batches API for large asynchronous processing jobs.
    • File Reuse: Use the Files API when the same document or image needs to be referenced across multiple requests.
    • Use Cases: This SDK is recommended when you need raw messages.create(...) access, require sync/async Python clients, or are implementing streaming, tool use, batches, or the Files API directly.