image 75

Force Claude 3 to Give You Perfect JSON Every Time

The Flaky AI Intern

I once built an automation to read customer feedback emails and add them to our project management tool. I gave a powerful LLM a simple prompt: “Extract the user’s name, email, and a one-sentence summary of their feedback. Format it as JSON.”

It worked beautifully… 80% of the time. The other 20%? Chaos.

Sometimes it would add a friendly intro like, “Sure, here is the JSON you requested!” which would break my code. Sometimes it would forget a field. Once, it decided the user’s name was “Sincerely,” because that’s how they signed the email. My automation pipeline was constantly breaking because I had hired a brilliant but hopelessly flaky intern. It could write poetry, but it couldn’t reliably fill out a simple form.

This is the silent killer of AI automation: unreliable outputs. If you can’t trust the data you get back, you can’t build a business on it. Today, we fix that. Permanently.

Why This Matters

The difference between asking an LLM to *describe* information and forcing it to *structure* information is everything. It’s the difference between asking an intern to scribble notes on a napkin versus handing them a mandatory, pre-printed form with clearly labeled boxes.

The napkin is fast, but messy and unreliable. The form is rigid, predictable, and perfect for feeding into a machine. Claude 3’s “Tool Use” feature (also known as function calling) is how you hand the AI that mandatory form.

This workflow replaces:

  • Fragile Parsing Scripts: No more writing complex code to clean up the AI’s messy text output.
  • Unreliable Prompts: You can stop begging the AI with phrases like “PLEASE only respond with JSON!”
  • Manual Data Entry: It transforms unstructured requests (emails, chats) into perfectly structured data ready for any system (CRM, database, calendar).

This is the bridge between the AI’s creative brain and the rigid, logical world of software. Mastering this is non-negotiable for building serious automations.

What This Tool / Workflow Actually Is

Let’s clear up a huge misconception. “Function calling” or “Tool Use” does NOT mean the AI runs your code. It can’t delete your files or call a real API on its own.

Instead, it’s a structured output feature.

You give Claude a list of “tools” it can use. Each tool is just a JSON schema—a blueprint for a function you have somewhere else. You describe what the tool does (e.g., “gets the current weather”) and what parameters it needs (e.g., a `location` string).

When you give Claude a prompt, it can choose to respond not with text, but with a request to use one of your tools, filling in the parameters perfectly according to your schema. It essentially says, “I believe the correct action is to call the `get_weather` function with `location` set to ‘San Francisco’.”

You get back a pristine JSON object. Guaranteed. What you do with that JSON is up to you.

Prerequisites

This is easier than it sounds. You just need two things:

  1. An Anthropic API Key: Go to the Anthropic website, create an account, and find your API key in the dashboard settings. This isn’t free, but it’s incredibly cheap for this kind of work. We’re talking fractions of a cent per request.
  2. A way to make an API call: We’ll use the simple command-line tool curl, which is pre-installed on Mac and Linux (and available on Windows). If you hate the terminal, you can use a graphical tool like Postman or Insomnia. I’ll give you the exact code to copy-paste.

That’s it. No libraries, no servers, no complicated setup.

Step-by-Step Tutorial

Let’s build a simple tool that extracts a user’s contact information from a sentence.

Step 1: Get Your API Key

If you haven’t already, sign up at console.anthropic.com, go to “API Keys” and create one. Copy it and keep it safe.

Step 2: Define Your Tool Schema

This is the “form” we’re giving the AI. It’s a JSON object that describes our imaginary function. Our function will be called `save_user_contact` and it needs a `name`, `email`, and an optional `phone` number.

This is what that schema looks like in JSON:

{
  "name": "save_user_contact",
  "description": "Extracts and saves a user's name, email, and phone number.",
  "input_schema": {
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "The full name of the user."
      },
      "email": {
        "type": "string",
        "description": "The user's email address."
      },
      "phone": {
        "type": "string",
        "description": "The user's phone number."
      }
    },
    "required": ["name", "email"]
  }
}

Notice we defined what each field is and even made `name` and `email` required. This is our contract with the AI.

Step 3: Construct the API Call with `curl`

Now we combine the API key, our prompt, and our tool schema into a single API request. This looks intimidating, but it’s just copy-paste. We’re sending our prompt and the list of available tools to the Claude 3 Sonnet model.

IMPORTANT: Replace `YOUR_ANTHROPIC_API_KEY` with your actual key.

curl https://api.anthropic.com/v1/messages \\
     -H "x-api-key: YOUR_ANTHROPIC_API_KEY" \\
     -H "anthropic-version: 2023-06-01" \\
     -H "content-type: application/json" \\
     --data '{
        "model": "claude-3-sonnet-20240229",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hi, my name is Jane Doe and my email is jane.doe@example.com. You can also reach me at 555-123-4567."}
        ],
        "tools": [
            {
                "name": "save_user_contact",
                "description": "Extracts and saves a user's name, email, and phone number.",
                "input_schema": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string", "description": "The full name of the user."},
                        "email": {"type": "string", "description": "The user's email address."},
                        "phone": {"type": "string", "description": "The user's phone number."}
                    },
                    "required": ["name", "email"]
                }
            }
        ]
     }'
Step 4: See the Perfect JSON Response

When you run that command, Claude won’t reply with text. It will reply with a JSON payload. The important part is the `content` block, which will look like this:

...
"content": [
  {
    "type": "tool_use",
    "id": "toolu_01A09q90qw90lq91789",
    "name": "save_user_contact",
    "input": {
      "name": "Jane Doe",
      "email": "jane.doe@example.com",
      "phone": "555-123-4567"
    }
  }
],
"stop_reason": "tool_use"
...

Look at that! The `stop_reason` is `tool_use`, and the `input` block contains the perfectly structured data we need. No chatter, no mistakes. This is data you can build a reliable system on.

Complete Automation Example
The Scenario: Booking a Meeting from an Email

Your team gets an email that says: `”Hey team, let’s schedule the ‘Q4 Planning Session’ for next Friday at 2pm. Please invite michael@ourcompany.com and support@ourcompany.com. We’ll need 60 minutes.” `

Our goal is to turn this into a structured object that a Google Calendar API can understand, without any manual work.

The Workflow:

We’ll define a tool called `create_calendar_event` with parameters for `title`, `start_time`, `duration`, and `attendees` (as an array of strings).

Here’s the full `curl` command to do it. Just replace the API key and run.

curl https://api.anthropic.com/v1/messages \\
     -H "x-api-key: YOUR_ANTHROPIC_API_KEY" \\
     -H "anthropic-version: 2023-06-01" \\
     -H "content-type: application/json" \\
     --data '{
        "model": "claude-3-sonnet-20240229",
        "max_tokens": 1024,
        "messages": [
            {"role": "user", "content": "Hey team, let\\'s schedule the \\'Q4 Planning Session\\' for next Friday at 2pm. Please invite michael@ourcompany.com and support@ourcompany.com. We\\'ll need 60 minutes."}
        ],
        "tools": [
            {
                "name": "create_calendar_event",
                "description": "Creates a new event in the user\\'s calendar.",
                "input_schema": {
                    "type": "object",
                    "properties": {
                        "title": {"type": "string", "description": "The title of the calendar event."},
                        "start_time": {"type": "string", "description": "The start time of the event in ISO 8601 format.", "example": "2024-10-26T14:00:00"},
                        "duration_minutes": {"type": "integer", "description": "The duration of the event in minutes."},
                        "attendees": {"type": "array", "items": {"type": "string"}, "description": "A list of email addresses for the attendees."}
                    },
                    "required": ["title", "start_time", "duration_minutes"]
                }
            }
        ]
     }'

Claude will analyze the text, figure out what “next Friday at 2pm” means relative to today, and give you back a perfect `tool_use` block with the structured data, ready to be passed to the next step in your automation.

Real Business Use Cases (MINIMUM 5)

This pattern is a superpower for business automation:

  1. SaaS Onboarding: A user in a chat says, “I want to create a new project called ‘Website Redesign’ for the marketing team.” An automation uses a `create_project` tool to extract `{ “project_name”: “Website Redesign”, “team_id”: “marketing” }` and creates it in the database.
  2. E-commerce Order Management: A customer emails, “I need to change the shipping on order #A123 to a P.O. Box.” A `update_shipping` tool extracts `{ “order_id”: “A123”, “new_address_type”: “PO_BOX” }` to flag it for manual review.
  3. Financial Services: A client requests, “Please buy 10 shares of AAPL for my retirement account.” A `execute_trade` tool extracts `{ “ticker_symbol”: “AAPL”, “quantity”: 10, “account_id”: “retirement” }` and queues it for execution.
  4. Content Management: A writer uploads a document and says, “Publish this article under the ‘Technology’ category with tags ‘AI’ and ‘Automation’.” A `publish_article` tool extracts `{ “category”: “Technology”, “tags”: [“AI”, “Automation”] }` to update the CMS.
  5. Logistics: A warehouse manager messages, “We received a shipment of 50 units for SKU #XYZ-987, but 3 were damaged.” A `log_inventory_event` tool extracts `{ “sku”: “XYZ-987”, “quantity_received”: 50, “quantity_damaged”: 3 }`.
Common Mistakes & Gotchas
  • Thinking Claude runs the code. I’ll say it again: It doesn’t. It just prepares the payload for you to run. You still need to write the code that actually creates the calendar event.
  • Bad descriptions. The AI relies heavily on the `description` fields for the tool and its parameters. Be clear and concise. A bad description like “Does stuff” will get you bad results. A good one like “Fetches a customer’s order history by their unique customer ID” is perfect.
  • Not handling the non-tool case. Sometimes, Claude will decide not to use a tool and will just respond with text. Your code needs to check if the `stop_reason` is `tool_use`. If it’s `end_turn`, you just got a regular text response.
  • Overly complex schemas. Start simple. Don’t try to define a tool with 50 parameters. Break complex tasks down into smaller, simpler tools.
How This Fits Into a Bigger Automation System

This is the central nervous system of a sophisticated automation. The structured data you get from a `tool_use` call is the perfect fuel for other systems:

  • Workflow Tools (n8n/Zapier): You can have a workflow that starts with an email, makes a `POST` request to the Claude API with your tool definitions, and then uses the perfectly structured JSON response in the next step to update a Salesforce record or send a Slack message.
  • Voice Agents: A user speaks a command. The audio is transcribed to text. That text is sent to Claude with a set of tools. Claude returns the structured command, which is then used to control smart home devices, book appointments, or query a database.
  • Multi-Agent Systems: This is how AI agents talk to each other and to the outside world. One agent (the “planner”) decides a task needs doing. It uses a tool call to format a request. Another agent (the “executor”) receives that structured request and performs the action.
What to Learn Next

You now have the single most reliable way to get structured data out of an LLM. You’ve solved the input problem. You can turn messy human language into clean, predictable machine language.

So… now what?

Getting the JSON is only half the battle. The next step is to *do something* with it automatically. In our next lesson, we’re going to build a full circle automation. We will create a system that not only uses a tool call to get structured data, but then takes that data, calls a *second* real-world API (like a CRM or database), gets a result, and then feeds that result *back* to Claude to generate a final, human-readable summary.

You’re about to learn how to make AI not just think, but act.

“,
“seo_tags”: “claude 3, function calling, ai automation, structured data, json output, anthropic api, claude tools, reliable automation, api integration, business process automation”,
“suggested_category”: “AI Automation Courses

Leave a Comment

Your email address will not be published. Required fields are marked *