Unlock Plus AI learning and gain exclusive insights from industry leaders
Access exclusive features like graded notebooks and quizzes
Earn unlimited certificates to enhance your resume
Starting at $1 USD/mo after a free trial โ cancel anytime
In the first lesson, you saw how skills work with Claude AI. Now, we'll work with the Claude API to test the two skills we made from the previous lesson. To use skills with the Claude API, we'll need to use the code execution tool and the files API. This will equip Claude with file system access for reading and writing files and with bash for executing code. Let's get to it. We've talked quite a bit about how skills work and how to create them. And we talked a little bit as well about the portability of skills across different environments in the Claude ecosystem, as well as many other agentic applications. We started by looking at skills in Claude AI and Claude Desktop, and now we're going to move to talk about how to use skills using the Claude Messages API. There are two things that are important to note. First, skills that you create in Claude AI and Claude Desktop are not shared in The Claude API or Claude Code. The second important piece is that in order for skills to work, we need the ability for Claude to execute code, create and edit documents, presentations, PDFs, and data reports, and work with a file system. This is something that we're going to have to manually do when we work with the Claude API. And this is something that is actually configured for you right away when using Claude AI and Claude Desktop. In Claude Desktop or Claude AI If I go to settings and I take a look at the capabilities, you can see here that there's a section for code execution and file creation. This is what we're going to talk about in more depth when we work with the API directly. But this is a setting that is enabled by default that allows Claude to execute code, create docs, spreadsheets, presentations, and more. This essentially gives Claude AI and Claude Desktop a computer or a virtual machine to execute code and perform all those tasks that make Skills happen. If this is disabled, we'll actually see that we need to turn this on to even be able to use skills. Now let's shift back and talk a little bit about how this code execution tool and file creation works, because we're going to need to enable this manually when we work with the API. When working with tools like Claude Code and the Claude agent, you have direct access to a file system. Whereas using the Claude API, we do not, and need a container to execute code and a file system to work with. Claude AI and Claude Desktop, that containerized environment and file system is given to you and not something you have to implement. At the end of the day, the functionality is all the same, but the way in which we utilize skills is slightly different. The skills themselves do not change, the format of those skills do not change, but depending on the environment that you're in, you may utilize the way in which skills work slightly differently. As we start to explore the Messages API, we're going to use the code execution tool. The code execution tool allows Claude to run Bash or shell commands to perform all these actions that we saw when working with skills. Creating, viewing, editing files, and writing code, all in a sandboxed environment. The code execution tool gives our application the ability to have a separate dedicated container to execute code and work with a file system. as you've seen with all the things that skills can do, that is mission critical for reading our skills, executing code within those skills, and working with other files that we might want to edit and view and create. To give you a visualization of what this looks like, when we include the code execution tool, we give Claude an execution sandbox or a container. When we ask Claude to create and execute files, these are executed in a safe and isolated environment. There are limitations for the RAM, the disk, the CPU, and more importantly, there is no internet connection provided and there are pre-installed libraries that you get out of the box. So this does not work with every single kind of coding environment. There are some limitations here to be mindful of. At the same time, we also get access to a file system that we can start adding directories to. You might have even seen hints of that when we worked with Claude desktop and Claude AI. This limitation of no internet connection is something that is specific to the Messages API. When we're using the code execution tool in Claude AI or Claude desktop, we do have access to an internet connection and we can download and install packages. The Code Execution Tool works quite nicely with another set of APIs that the Claude API allows us to work with. As you can imagine, when we're working with files, adding, creating, writing, modifying files, we need some mechanism for actually storing those underlying files. The Claude API includes a set of APIs called the Files API to upload and download files that can be run and worked on inside of the container. You can imagine a scenario where the user asks to summarize some input and save the summary to a text file. We upload that input file, send it to the container, download generated files with this Files API. We're going to be seeing this shortly in code. When we see the IDs that we get back from uploading and downloading files and how this works nicely with skills and our Code Execution Tool. And this is exactly where skills come into play. The library of skills that we get out of the box in tools like Claude AI or that we can include if we want using the API, those live in a directory that are powered in the container. As we start to read from this skills directory, as we start to add information to our skills or use those underlying skills to create new files that we can download or upload, this is where skills come into play. And we're going to see a requirement when working with the API, when we want to use skills, we need to use the Code Execution Tool as well. Now that we have a good sense of what the Code Execution Tool and Files API allow us to do, let's see how to use this in action. We're going to go and revisit the two previous custom skills that we built for generating practice questions as well as time series analysis. So let's head over to a Jupyter notebook and explore this. Right here, I have my two custom skills that we've worked with before. I also have a folder for data that I'm going to be using to analyze time series data. I also have a folder for lecture notes that I'll be using when I use my generating practice questions skill. To get started in this notebook, I'm going to load the environment variables that I need as well as a helper to help me find particular files from a directory. We're going to see this in action when we start using our skills. To start, I'm going to begin using my generating practice questions skill. So let's go ahead and take a look at the first part that I need to do. To begin, I need to upload the skill directory. Here you can see we're using that files_from_dir helper function, as well as the necessary beta headers for skills. Once this is done, I should be able to see the skill ID that I've created. This betas list are particular headers that I add when I make a request to the Messages API. Under the hood, these are turning into request headers to make sure that I'm getting the right data back and communicating appropriately with the API. To take a look at all the skills that I have, I can use this .list method, and I'm going to pass in a source of custom so that we don't load all the built-in skills and instead just confirm that I've created the ones as expected. And here I can see the title, as well as the unique skill ID that I'm going to be using shortly. In order for this to work as expected, we're going to need to make use of the LaTeX file where we're going to generate practice questions from. Here, I'll use the Files API to upload this particular LaTeX file. make sure that it's set for reading and then get back a file object. I'll be using this file object in conjunction with the skills necessary to make sure that it's all working as expected. I'm using Sonnet here, and I'm passing in the necessary beta headers, not only for skills, but in order for skills to work as expected when talking to the model, I need to make sure I have the code execution beta as well. And since I'm sending a file here, we have to make sure we have the files API header as well. When working with skills, these skills are set in a keyword argument called container, and here is where I pass in the list of skills. These could be custom ones or built-in ones. As I create many different versions of the skills, I can reference a particular timestamp or just use the latest one that I have. As I start to communicate with the model, I ask it to generate practice questions and then specify the file that I'm working with. This file object was previously created when I uploaded the LaTeX file. We finally make sure we're bringing in the correct tools for code execution. and send a message to our API. Now let's go take a look at the response that we got back. We can see here that there are multiple different pieces being used. Tools on the server, code execution, additional tools being used, and then finally, a bash code execution result. To make this a little bit cleaner to look at, let's add some nice formatting so that we can go ahead and take a look and analyze different text responses and tool use. We're going to go ahead and see in this particular series what's happening one step at a time. When we take a look at what the response is, which includes our text and our tool use and tool results, the first thing the model is telling us is it can help generate questions from these notes and it's going to start by reading the skill file and examining the lecture notes. Notice here, it's detected the skill that it needs to use, but it's only reading the SKILL.md We're going to see later on if there are additional files that need to be read, we'll make use of that progressive disclosure. We're also going to review in our input that LaTeX file as well. We're going to go ahead and see the underlying data that comes from these files. This is the YAML front matter that we've seen before, as well as the LaTeX from our notes04.tex file. Next, we're going to go ahead and check the markdown template to use the proper structure because we want our output to be in markdown. Here is where we're going to leverage a bit more of that progressive disclosure. Here's where we're going to read inside of the assets folder that markdown_template.md We'll get back the response that we've read and now we'll generate the questions based on the lecture notes that we've passed. Here we're going to use our code execution tool to create a particular file. We'll give that file text in markdown, and we'll get back the result of that file. We're going to go ahead and copy that to an output directory and use our Files API to get back a file_id that we can download later on. Once we get back that result, We can take a look at the underlying file that's been generated and make use of that file ID to programmatically download it. We can see here it's been saved and is ready for use. Using that file ID that we saw above Let's go ahead and download the file. We'll go ahead and check in this response and make sure that we have the file ID correctly extracted. And if we have that, which we expect to do, we should be able to download that particular file. We'll go ahead and write to a file called notes04.md with that content. includes the file ID as well as the necessary beta headers to communicate with the API. We can see here, we've downloaded that notes04.md file, and this is coming from the Files API with the code execution tool, all generated with the model and a skill. Inside of this file that we've downloaded, we can see that we're following those exact parts that we had in the skill. Starting with true and false questions, moving on to explanatory questions, to coding questions, and finally, to use case applications. We can preview this in markdown to see what that would look like. And here we can see our use case application, all the things necessary. Now is a good time to evaluate this particular output. Did we do exactly what the skill wanted? It looks good to start. bringing in some unit tests can really take this to the next level. If we need, we can go back and modify the skill, just like we saw before, using the API, the code execution tool, and the Files API as well. We also have the ability to delete skills programmatically. In order to delete a skill, we first have to find all of the versions associated with that skill and then delete them. Once those versions are deleted, we should be able to delete the underlying skill. Right here. Next, we're going to go ahead and use our analyzing time series skill alongside another skill. This is going to look pretty familiar to what we saw above, so let's go through these steps. First, we're going to upload our custom skill. get back a skill ID and confirm that we've done that as expected. Here, we can also see that we're not loading only the custom skills, we can see the built-in skills as well. These should look pretty familiar, as we saw them as well in Claude AI. Next, we're going to go ahead and upload our input file. This is going to be our retail sales CSV file. We're going to build a message to send to the API, and just like before, we're going to go ahead and use our skill, but here we're also going to include the docx skill as well. We're going to use this because we want to create a word doc summarizing the results and the plots. So here we're seeing a combination of custom skills with the skill ID that we have as well as the version. and using Anthropic built-in skills in this case, the docx skill. We're passing in the same headers that we had to pass in before, skills, code execution, and the files API. Now that this is finished running, we can examine the particular type of response that we get. We're going to see something similar to what we saw before, but this time there's just a little bit more happening. Let's go and see what's happening under the hood with our nice formatting. So here, the model is going to respond by helping us analyze time series data. And just like before, we're going to start reading the entirety of these SKILL.md files. We're going to read our custom skill as well as the built-in docx skill, which we're going to need to use. We can see the result of those include the content, starting from the beginning of the file and including the entire SKILL.md Next, we're going to go ahead and examine the data to run our time series analysis. We're going to look at just the first 20 lines of this CSV to examine the names of the columns and the type of data that we're working with. Since this is working as expected, we're going to go ahead and run the diagnostics and create the visualizations. These particular commands that we need to run are coming directly from our skill. Here is where we're going to go ahead, read those underlying files, execute that code and hand that back to Claude to work with. We're going to get back the result of these executions. We're going to get that back for diagnostics as well as visualize. We're then going to read the summary and diagnostics, which is the result of our script that comes in a file called summary.txt Once we have that particular file created, we can then go ahead and create a Word document. The built-in docx skill includes the correct content for how to work with Word docs. We're going to go ahead and take a look at how best to generate that document and leverage progressive disclosure here. We don't need everything from the docx skill, just using a way to get to those markdown files. Once we have that, we'll create a comprehensive Word document using the skill necessary, execute the code to make it do that, and generate the underlying Word document. Once we have that Word document, we'll copy that to the output directory and just like we saw before, get back a file ID that we can use if we want to download this Word document. We can see a summary of what this data looks like. And now we can download the file. Similarly, we'll go ahead and find that file_id if it exists. We're going to go ahead and download that particular file with the necessary contents as a docx file. If we take a look at what this looks like, we now have a Word document with our findings, our overview, our statistics. We can see that we brought in those plots and visualizations, as well as the statistical analysis that we've requested. This is also a great time to not only evaluate if what we're doing is expected in the skill, but that we're doing this in a predictable fashion. As we continue to evaluate, we can always modify this skill as much as we want. But all this data is coming in from the skill as well as the docx skill. to create this individual file. Like we saw before, if we want to delete this skill, we can list all the versions and delete all those versions. And once those versions are deleted, delete the underlying skill. In this lesson, We've combined our knowledge of the Messages API, the Code Execution tool, the Files API, and skills to take our custom skills and work with them programmatically. In the next lesson, we're going to move to Claude code and see how to add our own custom skills inside of a .claude folder and build a more sophisticated command line application.