Build Your Own AI Dev Tools with the Claude Code SDK

Standard AI SDKs are powerful for text generation, but they can't read your local files, understand your code's context, or write changes back to your project. This lesson demonstrates how to break past that limitation using the @anthropic-ai/claude-code SDK, which leverages the full power of the Claude Code executable installed in your environment.

You'll learn how to create simple yet powerful scripts that are aware of your codebase and can perform actions like reading files and writing output, all driven by natural language prompts.

Workflow demonstrated in this lesson:

  • Initialize a new project with Bun and install the @anthropic-ai/claude-code SDK.
  • Use the query function to stream responses from the AI.
  • Filter streamed events to isolate and handle the final successful text result.
  • Craft prompts that instruct the AI to read the script's own source code and analyze its contents.
  • Grant permissions to the AI to perform file system actions, such as writing its findings to a new text file.

Key benefits:

  • Go Beyond Text Generation: Build scripts that can read, understand, and interact with your project files.
  • Leverage Built-in Tool Use: Directly access Claude Code's ability to perform actions without complex API integrations.
  • Automate Code Analysis: Create tools that can analyze variables, summarize files, or perform other code-related tasks with a simple prompt.
  • Secure by Default: Learn how to explicitly grant permissions for actions like file editing, ensuring you remain in control.
Share with a coworker

Transcript

[00:00] Use bun init to initialize our project. It'll be blank. And then we're going to bun install the anthropic-ai-clod-code. Now we can check this is working with bun run and run it against the index. So we get hello via bun.

[00:15] Then I can open the index file in cursor you can see the log here. So we're going to import query from the Cloud Code API and once we call query with a prompt and our prompt can just be say hello this is going to return a generator. Now we're going to iterate through the generator with for await, which was automatically suggested, and we'll log out all of the chunks coming from the generator. So once we run bun run index you'll see we get the initial message which is a system message with all of the available tools and the model we'll get an assistant message and then we'll get our result message with a subtype of success and the final result. So this is what we really care about when the goal is to generate text.

[01:01] So to grab this out we'll close this. We'll create a variable for our result text and just set it to an empty string. And we will check if the chunk is a type of result and it's a subtype of success. Then we will assign the result text to the chunk.result. Now the subtype is important because if you remove this the SDK result message only has a result when the subtype is success.

[01:27] So now if we run the exact same thing I'll clear this out, run the same bun run index, you'll see nothing while it's waiting, and then the final result. Now note that I didn't have to log in or anything because I already have Cloud Code installed and I am authenticated. So it's using the same binary when executing Cloud Code and this SDK is essentially wrapping around your standard CLOD commands and just using the results in your scripts. And because it is CLOD code you can write prompts like read the index.ts, list all of the variables, and you don't have to add any more functionality to this script because once you run it we'll sit here and wait for it to finish. We'll get the results of generator, result, text, and chunk which are all of our variables in this script.

[02:11] Now if you change the prompt to write the result to variables.txt and try running it again you'll get text in the final output that it needs permission to write files. Essentially it attempted to do it, found it didn't have permission, so it just output the text that it could. Although this doesn't give you great information on how to give it permissions. For now in the options we are going to turn on permission mode to accept edits, and accepting edits gives you enough permission to allow it to write files. So if we check over in our variables.txt You'll see the result from this created file.