C Programming Using Claude’s New Computer Use Model
How well can Sonnet write and run code on your behalf?
As you have probably heard by now, Claude released a couple of major news stories a few days ago.
One was about a new 3.5 Haiku model that promises to emulate the abilities of the current 3.0 Opus model but should be much quicker and cheaper to use. This was a “coming soon” announcement, as the new model won’t actually be released until sometime in November.
The second major talking point and the one which has sent the AI world into a bit of a frenzy is the news that the upgraded Sonnet 3.5 model also has a new ability.
Called Computer Use, the model can now control a PC desktop.
It does this in a very natural, human-like way.
By that, I mean that it can open windows and applications, point and click using the mouse and enter text, search the web using Google, etc. …
At this stage, Computer Use is experimental and prone to errors, but is an exciting prospect nonetheless.
Now, you don’t need to be a rocket scientist to recognise that giving AI this kind of ability could be slightly concerning. Claude has kicked that particular can down the road a bit by ensuring that, for now, the new model can only do this in a sandboxed virtual environment via Docker.
What is docker?
Docker is an open-source platform designed to automate the deployment, scaling, and management of applications using containerization. Containers are lightweight, portable, and self-sufficient and separate environments that bundle an application’s code, dependencies, libraries, and configurations, allowing it to run consistently across different computing environments.
Docker enhances application security by isolating apps in containers, ensuring that each app runs in its own environment, and minimising risks from vulnerabilities and dependencies. Additionally, Docker containers have built-in security features like namespaces and control groups, which help restrict access to system resources, reducing the potential impact of security breaches.
So, to use this new model ability, the first thing you’ll need to do is install and run docker if you don’t already have it.
Installing docker
Head over to,
Click the big blue Download Docker Desktop button and choose the appropriate installation type for your system.
When the installation file has finished downloading, click on it to begin the installation process and follow the on-screen prompts.
At the end, you’ll be prompted to enter your contact details. Ensure you opt to sign in to Docker in a Personal capacity. After that, you should click the Proceed to Docker Desktop button and Docker should be up and running.
As we’ll be doing some coding, the second thing you’ll need is an Anthropic API key. You can get that from the Anthropic Console. Register or Sign-In, then you’ll see a screen like this,
Click the Get API Keys button and follow the instructions from there. Take note of your key, as you’ll need it in a bit.
Checking out the model’s coding abilities
I was immediately intrigued by the capabilities that this new model could offer, and right away I thought of how it might be used to code and run programs on our behalf. So, in the rest of this article, we’re going to explore that further.
Back in the day, I did a bit of C programming, so I thought it would be fun to revisit that and put the model through its paces.
I’ve two examples in mind. The first is just the ubiquitous hello world program, but the second is a bit more challenging — a sudoku puzzle solver.
To instruct the model in our coding challenges, we need to start it up using Docker.
The instructions for doing this are well documented in Anthropic’s Computer Use GitHub Demo. Click here to go there, but basically from a WSL2 Ubuntu Linux prompt I did the following,
- $ export ANTHROPIC_API_KEY=your_api_key
- $ docker run \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
-v $HOME/.anthropic:/home/computeruse/.anthropic \
-p 5900:5900 \
-p 8501:8501 \
-p 6080:6080 \
-p 8080:8080 \
-it ghcr.io/anthropics/anthropic-quickstarts:computer-use-demo-latest
The first time around, the docker command will take a minute or two to complete as it downloads everything it needs. Eventually, you’ll see this message,
Open up a browser and type the above URL into the address bar. That should kick off the app and look like this,
Testing the new model
- hello world
I typed the following into the prompt chat box near the bottom left of the screen.
Open a terminal window. Use vi to create a C program file that prints out the text “hello world” on the terminal
I won’t include all the screenshots that the model took, as there were loads, but here is the text it responded with.
I'll help you create a simple C "hello world" program. Since we need a
terminal, let's first take a screenshot to see the desktop and
locate the terminal icon.
I see a terminal icon in the taskbar. Let's click on it to open a
terminal window.
Now that we have a terminal open, let's create a C program using vi. I'll
type the vi command to create a new file called hello.c:
I see that vi is not installed. Let's install it first:
Now let's try creating our hello.c file again:
Now I'll enter insert mode in vi by pressing 'i' and type our hello world
program:
Now I'll save and exit vi by pressing Escape to exit insert mode, then
typing :wq and Enter:
Now let's compile and run the program. First, we need to make sure we have
gcc installed:
Now let's compile and run our program:
As you can see from the headline image for this article, Claude did indeed compile the C program it had produced and ran it successfully.
I think the most impressive part of the interaction is when the model realises it doesn’t have access to a particular program (vi in this case) and just goes ahead and installs it!
2. A sudoku solver
My prompt this time was,
Open a terminal and create a C program that solves 9x9 sudoku puzzles. The program should populate the sudoku grid with 5 random numbers in random locations as a starting point. Display this starting puzzle, then solve the puzzle and display the final solution.
Here are the outputs that were produced.
Now, let's create a C program that implements a Sudoku solver. First, let's
make sure we have gcc installed:
/usr/bin/gcc
Reading package lists...
Building dependency tree...
Reading state information...
gcc is already the newest version (4:11.2.0-1ubuntu1).
gcc set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Now let's create our Sudoku solver program. I'll use the str_replace_editor
to create the file:
File created successfully at: /home/computeruse/sudoku.c
Now let's compile and run the program:
Generating Sudoku puzzle with 5 random numbers...
Starting position:
+-------+-------+-------+
| . . . | . . 8 | . . 5 |
| . . . | . . . | . . . |
| . . . | . . . | . . . |
+-------+-------+-------+
| . . . | . . . | . . . |
| . . . | . . . | 1 . . |
| . . . | . . . | . . 9 |
+-------+-------+-------+
| . . . | . . . | . . . |
| . . . | . . . | . . . |
| . . . | . . . | 6 . . |
+-------+-------+-------+
Solved puzzle:
+-------+-------+-------+
| 1 2 3 | 4 6 8 | 7 9 5 |
| 4 5 6 | 1 7 9 | 2 3 8 |
| 7 8 9 | 2 3 5 | 4 1 6 |
+-------+-------+-------+
| 2 1 4 | 3 9 6 | 5 8 7 |
| 3 9 5 | 7 8 2 | 1 6 4 |
| 6 7 8 | 5 1 4 | 3 2 9 |
+-------+-------+-------+
| 5 6 1 | 8 2 7 | 9 4 3 |
| 9 3 7 | 6 4 1 | 8 5 2 |
| 8 4 2 | 9 5 3 | 6 7 1 |
+-------+-------+-------+
To show this wasn’t a fluke, I manually went into the terminal, compiled the code and re-ran it. Here is an image capturing that interaction.
The app went on to explain how the program worked. Before showing that, I also asked it to print out the full source code of the sudoku.c file it created.
Here's the result of running `cat -n` on /home/computeruse/sudoku.c:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 #define N 9
6 #define EMPTY 0
7
8 // Function to check if a number can be placed in a given position
9 int isSafe(int grid[N][N], int row, int col, int num) {
10 // Check row
11 for (int x = 0; x < N; x++) {
12 if (grid[row][x] == num) {
13 return 0;
14 }
15 }
16
17 // Check column
18 for (int x = 0; x < N; x++) {
19 if (grid[x][col] == num) {
20 return 0;
21 }
22 }
23
24 // Check 3x3 box
25 int startRow = row - row % 3;
26 int startCol = col - col % 3;
27 for (int i = 0; i < 3; i++) {
28 for (int j = 0; j < 3; j++) {
29 if (grid[i + startRow][j + startCol] == num) {
30 return 0;
31 }
32 }
33 }
34
35 return 1;
36 }
37
38 // Function to solve Sudoku using backtracking
39 int solveSudoku(int grid[N][N]) {
40 int row, col;
41 int isEmpty = 0;
42
43 // Find an empty cell
44 for (row = 0; row < N; row++) {
45 for (col = 0; col < N; col++) {
46 if (grid[row][col] == EMPTY) {
47 isEmpty = 1;
48 break;
49 }
50 }
51 if (isEmpty) {
52 break;
53 }
54 }
55
56 // If no empty cell found, puzzle is solved
57 if (!isEmpty) {
58 return 1;
59 }
60
61 // Try digits 1 to 9
62 for (int num = 1; num <= 9; num++) {
63 if (isSafe(grid, row, col, num)) {
64 grid[row][col] = num;
65 if (solveSudoku(grid)) {
66 return 1;
67 }
68 grid[row][col] = EMPTY;
69 }
70 }
71
72 return 0;
73 }
74
75 // Function to print the grid
76 void printGrid(int grid[N][N]) {
77 printf("\n+-------+-------+-------+\n");
78 for (int i = 0; i < N; i++) {
79 printf("|");
80 for (int j = 0; j < N; j++) {
81 if (grid[i][j] == 0)
82 printf(" .");
83 else
84 printf(" %d", grid[i][j]);
85 if ((j + 1) % 3 == 0)
86 printf(" |");
87 }
88 printf("\n");
89 if ((i + 1) % 3 == 0)
90 printf("+-------+-------+-------+\n");
91 }
92 }
93
94 // Function to generate a random starting position
95 void generateStartingPosition(int grid[N][N], int numClues) {
96 // Initialize random seed
97 srand(time(NULL));
98
99 // Clear the grid
100 for (int i = 0; i < N; i++) {
101 for (int j = 0; j < N; j++) {
102 grid[i][j] = EMPTY;
103 }
104 }
105
106 // Add random numbers
107 for (int i = 0; i < numClues; i++) {
108 int row, col, num;
109 do {
110 row = rand() % N;
111 col = rand() % N;
112 num = 1 + (rand() % 9);
113 } while (!isSafe(grid, row, col, num) || grid[row][col] != EMPTY);
114
115 grid[row][col] = num;
116 }
117 }
118
119 int main() {
120 int grid[N][N] = 0;
121
122 printf("Generating Sudoku puzzle with 5 random numbers...\n");
123 generateStartingPosition(grid, 5);
124
125 printf("\nStarting position:");
126 printGrid(grid);
127
128 if (solveSudoku(grid)) {
129 printf("\nSolved puzzle:");
130 printGrid(grid);
131 } else {
132 printf("\nNo solution exists.\n");
133 }
134
135 return 0;
136 }
And the model’s explanation of the code…
Perfect! The program works as expected. Let me explain the key components of
the program:
The program uses a 9x9 grid represented as a 2D array
Main functions:
isSafe: Checks if a number can be placed in a given position
solveSudoku: Implements the backtracking algorithm to solve the puzzle
printGrid: Displays the grid in a nice formatted way
generateStartingPosition: Creates a random starting position with 5 numbers
The solution process:
First, it generates a random starting position with 5 numbers
Then it displays this initial puzzle
Using backtracking, it finds a valid solution
Finally, it displays the solved puzzle
The output shows:
The initial puzzle with 5 random numbers placed (shown as numbers, while empty
cells are shown as dots)
The complete solution that satisfies all Sudoku rules:
Each row contains numbers 1-9 exactly once
Each column contains numbers 1-9 exactly once
Each 3x3 box contains numbers 1-9 exactly once
The program uses a backtracking algorithm which:
Finds an empty cell
Tries numbers 1-9 in that cell
Recursively attempts to solve the rest of the puzzle
If a solution is not possible, it backtracks and tries a different number
You can run the program multiple times to get different starting positions
and solutions, as it uses random number generation to create the initial
puzzle.
Would you like me to explain any specific part of the code in more detail?
I mean, just wow! How impressive is that?
Summary
I hate to jump on the bandwagon, but I have to agree with everyone else who’s written good things about the new Computer Use Claude model.
It is excellent, and I only looked at one small aspect of it, namely its coding abilities.
It can’t do everything you might want it to. For example, drag and drop appears to be beyond it just now, and it can struggle with high graphical resolutions.
There’s probably more work also required in terms of security and safety, but remember, this is only the beginning.
How might this develop going forward? Let me know your thoughts in the comments.
OK, that’s all for me just now. I hope you found this article useful. If you did, please check out my profile page at this link. From there, you can see my other published stories, follow me or subscribe to get notified when I post new content.
I know times are tough and wallets constrained, but if you got value from this article, please consider buying me a wee dram.
If you liked this content, I think you’ll also find these articles worth reading.
C Programming Using Claude’s New Computer Use Model was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.
from Datascience in Towards Data Science on Medium https://ift.tt/XAvP25Q
via IFTTT