<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Roi Gomez</title>
  <subtitle>Claude API, Claude Code, MCP and AI productivity</subtitle>
  <link href="https://roigomez.com/en/feed.xml" rel="self"/>
  <link href="https://roigomez.com/en/"/>
  <updated>2025-05-11</updated>
  <id>https://roigomez.com/en/</id>
  <author><name>Roi Gomez</name><email>roi.gomez@gmail.com</email></author>
  <language>en</language>

  
  <entry>
    <title>How I built an AI agent with Claude Code in 20 minutes</title>
    <link href="https://roigomez.com/en/blog/ai-agent-claude-code-20-minutes/"/>
    <id>https://roigomez.com/en/blog/ai-agent-claude-code-20-minutes/</id>
    <updated>2025-05-11</updated>
    <summary>A complete walkthrough: from zero to a working agent using only the Claude Code CLI. No frameworks, no magic.</summary>
    <content type="html"><![CDATA[<p>Claude Code is not just a glorified autocomplete. It’s an agent runtime with full access to your filesystem, terminal, and the ability to run any command. When you understand that, the possibilities shift.</p>
<h2 id="the-starting-point" tabindex="-1">The starting point</h2>
<p>I wanted a simple agent: monitor a <code>src/</code> directory, detect changes in <code>.ts</code> files, run the affected tests, and report the result. No external watchers. No CI. Just Claude doing the work.</p>
<p>The initial prompt was this direct:</p>
<pre class="language-bash"><code class="language-bash">claude <span class="token string">"Monitor the src/ directory. When you detect changes in .ts files,
run the related tests with npx jest and report the result with a summary
of what passed and what failed."</span></code></pre>
<p>Claude Code has <code>Bash</code> and <code>Read</code> tools built in. It knows how to run commands and read files. The agent worked on the first try — not because the prompt was perfect, but because the task matched the available tools.</p>
<h2 id="adding-persistence-with-claude.md" tabindex="-1">Adding persistence with <a href="http://CLAUDE.md">CLAUDE.md</a></h2>
<p>The problem with the previous agent is it loses context between sessions. The solution is a <code>CLAUDE.md</code> in the project root — Claude Code reads it automatically on startup.</p>
<pre class="language-markdown"><code class="language-markdown"><span class="token title important"><span class="token punctuation">#</span> Project: Users API</span>

<span class="token title important"><span class="token punctuation">##</span> Stack</span>
<span class="token list punctuation">-</span> TypeScript + Node.js
<span class="token list punctuation">-</span> Jest for testing
<span class="token list punctuation">-</span> PostgreSQL with Drizzle ORM

<span class="token title important"><span class="token punctuation">##</span> Test conventions</span>
<span class="token list punctuation">-</span> Tests live in <span class="token code-snippet code keyword">`src/__tests__/`</span>
<span class="token list punctuation">-</span> Filename: <span class="token code-snippet code keyword">`[feature].test.ts`</span>
<span class="token list punctuation">-</span> Run only the tests for the affected module: <span class="token code-snippet code keyword">`npx jest [filename]`</span>

<span class="token title important"><span class="token punctuation">##</span> Agent rules</span>
<span class="token list punctuation">-</span> If a test fails, check the error in logs before editing code
<span class="token list punctuation">-</span> Do not modify files outside <span class="token code-snippet code keyword">`src/`</span></code></pre>
<p>With this persistent context, the agent has the instructions it needs to operate correctly even across new sessions.</p>
<h2 id="the-result" tabindex="-1">The result</h2>
<p>The agent took about 3 minutes to set up and ran reliably for weeks. The key wasn’t prompt complexity — it was clarity: concrete task, known tools, explicit constraints.</p>
<p>What surprised me was Claude’s ability to chain actions: detect the changed file → identify which tests to run → run the tests → parse the output → report only failures with context. All of that without additional code on my end.</p>
<p>If you have a task that can be described in natural language and executed with terminal commands, you can probably automate it with Claude Code in less time than you think.</p>
]]></content>
  </entry>
  
  <entry>
    <title>CLAUDE.md: best practices for configuring your agent</title>
    <link href="https://roigomez.com/en/blog/claude-md-best-practices/"/>
    <id>https://roigomez.com/en/blog/claude-md-best-practices/</id>
    <updated>2025-05-06</updated>
    <summary>How to write an effective CLAUDE.md that makes Claude Code more useful and predictable in your project. Structure, examples, and common mistakes.</summary>
    <content type="html"><![CDATA[<p><a href="http://CLAUDE.md">CLAUDE.md</a> is the configuration file that Claude Code reads automatically when starting in a project. It’s your way of telling the agent how to behave, what conventions to follow, and what context it needs to be useful from the first message.</p>
<h2 id="what-goes-in-claude.md-and-what-doesn%E2%80%99t" tabindex="-1">What goes in <a href="http://CLAUDE.md">CLAUDE.md</a> and what doesn’t</h2>
<p><a href="http://CLAUDE.md">CLAUDE.md</a> is not technical documentation for the project. It’s a briefing for the agent. Include information Claude needs to operate well but can’t infer from reading the code.</p>
<p><strong>Include:</strong></p>
<ul>
<li>Tech stack and specific versions</li>
<li>Project code conventions</li>
<li>Development, test, and deploy commands</li>
<li>Restrictions and files it shouldn’t touch</li>
<li>Business or domain context that isn’t obvious</li>
</ul>
<p><strong>Don’t include:</strong></p>
<ul>
<li>Function documentation (that goes in the code)</li>
<li>Change history</li>
<li>Documentation for human users</li>
<li>Things Claude can infer from reading the code</li>
</ul>
<h2 id="recommended-structure" tabindex="-1">Recommended structure</h2>
<pre class="language-markdown"><code class="language-markdown"><span class="token title important"><span class="token punctuation">#</span> Project</span>

[2-3 sentences describing what the project does and for whom]

<span class="token title important"><span class="token punctuation">##</span> Stack</span>

<span class="token list punctuation">-</span> Runtime: Node.js 20
<span class="token list punctuation">-</span> Framework: Next.js 14 App Router
<span class="token list punctuation">-</span> Database: PostgreSQL via Drizzle ORM
<span class="token list punctuation">-</span> Tests: Vitest + Testing Library

<span class="token title important"><span class="token punctuation">##</span> Commands</span>

```bash
npm run dev          # dev server at :3000
npm run test         # tests in watch mode
npm run test:ci      # tests without watch (for CI)
npm run db:push      # sync schema with DB</code></pre>
<h2 id="conventions" tabindex="-1">Conventions</h2>
<ul>
<li>Components in PascalCase, hooks with <code>use</code> prefix</li>
<li>Absolute imports from <code>@/</code> (alias configured in tsconfig)</li>
<li>No <code>any</code> in TypeScript — use <code>unknown</code> if you need to</li>
<li>Tests next to the file they test, not in a separate folder</li>
</ul>
<h2 id="restrictions" tabindex="-1">Restrictions</h2>
<ul>
<li>Do not modify <code>src/migrations/</code> — migrations are auto-generated</li>
<li>Do not install dependencies without confirming first</li>
<li><code>src/lib/vendor/</code> is third-party code, don’t touch it</li>
</ul>
<pre><code>
## The most common mistake: vague instructions

Vague instructions don't help. &quot;Write clean code&quot; or &quot;follow best practices&quot; tells the agent nothing useful. Be specific about what clean means in your project.

Instead of:
</code></pre>
<p>Use good security practices.</p>
<pre><code>
Write:
</code></pre>
<p>Input validation: use Zod on all API endpoints.
Never build SQL queries with string concatenation — always use prepared parameters.
API keys go in environment variables, never hardcoded.</p>
<pre><code>
## CLAUDE.md in subdirectories

Claude Code reads all CLAUDE.md files in the directory hierarchy. You can have a root one with global config and additional files in subdirectories for specific context:

</code></pre>
<p>project/
├── <a href="http://CLAUDE.md">CLAUDE.md</a>              # global config
├── frontend/
│   └── <a href="http://CLAUDE.md">CLAUDE.md</a>          # frontend-specific conventions
└── backend/
└── <a href="http://CLAUDE.md">CLAUDE.md</a>          # backend conventions</p>
<pre><code>
The agent combines all relevant contexts based on the directory it's working in.

## Update CLAUDE.md when the project changes

CLAUDE.md is a living document. If you change the stack, add a new convention, or notice the agent consistently doing something you don't want, update the file. It's more efficient than correcting it in every session.

A well-maintained CLAUDE.md is the difference between an agent that needs constant guidance and one that operates well from the start.
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title>Prompt caching: how to cut Claude API costs by 80%</title>
    <link href="https://roigomez.com/en/blog/prompt-caching-reduce-costs/"/>
    <id>https://roigomez.com/en/blog/prompt-caching-reduce-costs/</id>
    <updated>2025-05-05</updated>
    <summary>The most underused feature in Anthropic&#39;s API. How it works, when to use it, and how much I saved.</summary>
    <content type="html"><![CDATA[<p>There’s one line of code that cut my Claude API bill by 78% in a single month. It’s called <code>cache_control</code>. Most people using the Anthropic API aren’t using it. That’s a mistake.</p>
<h2 id="what-prompt-caching-is" tabindex="-1">What prompt caching is</h2>
<p>When you call the Claude API, you pay for every token the model processes — both prompt tokens and response tokens. If you have a 2000-token system prompt you send with every request, you’re paying for those 2000 tokens every time.</p>
<p>Prompt caching changes that. You mark parts of the prompt as cacheable, and Anthropic stores them in their infrastructure for 5 minutes. Subsequent requests that use that cached portion cost roughly 90% less for that segment.</p>
<pre class="language-python"><code class="language-python"><span class="token keyword">import</span> anthropic

client <span class="token operator">=</span> anthropic<span class="token punctuation">.</span>Anthropic<span class="token punctuation">(</span><span class="token punctuation">)</span>

response <span class="token operator">=</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>create<span class="token punctuation">(</span>
    model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
    max_tokens<span class="token operator">=</span><span class="token number">1024</span><span class="token punctuation">,</span>
    system<span class="token operator">=</span><span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"text"</span><span class="token punctuation">,</span>
            <span class="token string">"text"</span><span class="token punctuation">:</span> <span class="token string">"You are an expert TypeScript assistant..."</span><span class="token punctuation">,</span>
            <span class="token string">"cache_control"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"ephemeral"</span><span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"Explain generics"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">)</span></code></pre>
<p>The <code>cache_control</code> field with <code>type: &quot;ephemeral&quot;</code> tells Anthropic: “cache everything up to this point.”</p>
<h2 id="when-it-makes-sense-to-use-it" tabindex="-1">When it makes sense to use it</h2>
<p>Caching is most effective when you have:</p>
<p><strong>Long, stable system prompts.</strong> A system prompt that defines agent behavior, includes documentation, or provides extensive context. If your system prompt is over 500 tokens and doesn’t change between requests, cache it.</p>
<p><strong>Reference documents or code.</strong> If you’re doing Q&amp;A over a document, code analysis, or any task requiring fixed context, that context should be cached.</p>
<p><strong>Long conversations.</strong> Previous messages in a conversation can be cached to avoid reprocessing the history.</p>
<p>Caching does NOT help if your prompts change constantly or are short (less than 1024 tokens for Sonnet — that’s the minimum for the cache to activate).</p>
<h2 id="the-real-numbers" tabindex="-1">The real numbers</h2>
<p>In a project where I used Claude to analyze code in a repo, I had a ~3000-token system prompt that included project conventions. Without cache: ~$0.18 per request (just the system prompt). With cache: $0.018 on the first request, $0.003 on subsequent ones.</p>
<p>For 500 requests per day, that goes from $90/day to $1.50/day for the system prompt alone. The savings pay for themselves within hours.</p>
<h2 id="one-important-detail" tabindex="-1">One important detail</h2>
<p>The cache lasts 5 minutes. If there’s a gap of more than 5 minutes between requests using the same cache, the next request pays full price and refreshes the cache. This matters for intermittent workloads.</p>
<p>To verify the cache is working, check the API response — the <code>usage</code> field includes <code>cache_creation_input_tokens</code> and <code>cache_read_input_tokens</code>. If <code>cache_read_input_tokens &gt; 0</code>, the cache is active.</p>
<p>Implementation takes under 10 minutes. There’s no reason not to do it.</p>
]]></content>
  </entry>
  
  <entry>
    <title>Build your first MCP server in TypeScript</title>
    <link href="https://roigomez.com/en/blog/first-mcp-server-typescript/"/>
    <id>https://roigomez.com/en/blog/first-mcp-server-typescript/</id>
    <updated>2025-04-28</updated>
    <summary>Model Context Protocol explained with real code. From zero to a working TypeScript server with the tools you need.</summary>
    <content type="html"><![CDATA[<p>MCP (Model Context Protocol) is the mechanism that lets Claude Code connect to external tools. An MCP server exposes functions that Claude can invoke, similar to how an LLM invokes function calls in the OpenAI API, but with a standardized protocol that works with any MCP client.</p>
<h2 id="what-an-mcp-server-actually-is" tabindex="-1">What an MCP server actually is</h2>
<p>An MCP server is a process that exposes a set of tools via a JSON-RPC protocol. Claude Code connects to it at startup and can invoke those tools during the session.</p>
<p>The most useful use cases: connect Claude to your database, expose internal APIs, give it access to services it doesn’t have natively (Slack, Jira, your own CMS).</p>
<h2 id="initial-setup" tabindex="-1">Initial setup</h2>
<pre class="language-bash"><code class="language-bash"><span class="token function">mkdir</span> my-mcp-server <span class="token operator">&amp;&amp;</span> <span class="token builtin class-name">cd</span> my-mcp-server
<span class="token function">npm</span> init <span class="token parameter variable">-y</span>
<span class="token function">npm</span> <span class="token function">install</span> @modelcontextprotocol/sdk
<span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">-D</span> typescript @types/node ts-node</code></pre>
<pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"compilerOptions"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"target"</span><span class="token operator">:</span> <span class="token string">"ES2022"</span><span class="token punctuation">,</span>
    <span class="token property">"module"</span><span class="token operator">:</span> <span class="token string">"CommonJS"</span><span class="token punctuation">,</span>
    <span class="token property">"outDir"</span><span class="token operator">:</span> <span class="token string">"dist"</span><span class="token punctuation">,</span>
    <span class="token property">"strict"</span><span class="token operator">:</span> <span class="token boolean">true</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="the-minimal-server" tabindex="-1">The minimal server</h2>
<pre class="language-typescript"><code class="language-typescript"><span class="token keyword">import</span> <span class="token punctuation">{</span> Server <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"@modelcontextprotocol/sdk/server/index.js"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> StdioServerTransport <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"@modelcontextprotocol/sdk/server/stdio.js"</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> CallToolRequestSchema<span class="token punctuation">,</span> ListToolsRequestSchema <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"@modelcontextprotocol/sdk/types.js"</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> server <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Server</span><span class="token punctuation">(</span>
  <span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">"my-tools"</span><span class="token punctuation">,</span> version<span class="token operator">:</span> <span class="token string">"1.0.0"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span> capabilities<span class="token operator">:</span> <span class="token punctuation">{</span> tools<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">setRequestHandler</span><span class="token punctuation">(</span>ListToolsRequestSchema<span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token punctuation">{</span>
  tools<span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
      name<span class="token operator">:</span> <span class="token string">"get_timestamp"</span><span class="token punctuation">,</span>
      description<span class="token operator">:</span> <span class="token string">"Returns the current timestamp in ISO format"</span><span class="token punctuation">,</span>
      inputSchema<span class="token operator">:</span> <span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span> properties<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span> required<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

server<span class="token punctuation">.</span><span class="token function">setRequestHandler</span><span class="token punctuation">(</span>CallToolRequestSchema<span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span>request<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>request<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name <span class="token operator">===</span> <span class="token string">"get_timestamp"</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">{</span>
      content<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> type<span class="token operator">:</span> <span class="token string">"text"</span><span class="token punctuation">,</span> text<span class="token operator">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toISOString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">Tool not found: </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>request<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> transport <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StdioServerTransport</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> server<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span>transport<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<h2 id="connecting-it-to-claude-code" tabindex="-1">Connecting it to Claude Code</h2>
<p>In your <code>.claude/settings.json</code>:</p>
<pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"mcpServers"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"my-tools"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"node"</span><span class="token punctuation">,</span>
      <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"./dist/index.js"</span><span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>After <code>npx tsc &amp;&amp; claude</code>, Claude Code has access to your <code>get_timestamp</code> tool. From there, add whatever tools you need: database queries, API calls, reading config files.</p>
<p>The MCP SDK handles all the protocol. Your job is just to define the tools and their logic.</p>
]]></content>
  </entry>
  
  <entry>
    <title>Extended thinking in Claude: when and how to use deep reasoning</title>
    <link href="https://roigomez.com/en/blog/extended-thinking-claude/"/>
    <id>https://roigomez.com/en/blog/extended-thinking-claude/</id>
    <updated>2025-04-22</updated>
    <summary>Extended thinking lets Claude show its reasoning process before responding. Practical guide to enabling it and getting the most from it on complex tasks.</summary>
    <content type="html"><![CDATA[<p>Extended thinking is the mode where Claude “thinks out loud” before giving a response. Instead of jumping straight to an answer, Claude works through the problem internally and you can see that process. For complex tasks, the quality difference is significant.</p>
<h2 id="how-it-works-internally" tabindex="-1">How it works internally</h2>
<p>When you enable extended thinking, Claude generates a <code>thinking</code> block before the <code>text</code> block of the response. This block contains the internal reasoning: hypotheses, checks, self-corrections. It’s not decorative — it’s the real process that improves the final response.</p>
<p>Thinking consumes output tokens but Anthropic bills it differently: thinking tokens cost the same as regular output tokens but don’t count toward context limits the same way.</p>
<h2 id="enabling-extended-thinking-in-the-api" tabindex="-1">Enabling extended thinking in the API</h2>
<pre class="language-python"><code class="language-python"><span class="token keyword">import</span> anthropic

client <span class="token operator">=</span> anthropic<span class="token punctuation">.</span>Anthropic<span class="token punctuation">(</span><span class="token punctuation">)</span>

response <span class="token operator">=</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>create<span class="token punctuation">(</span>
    model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
    max_tokens<span class="token operator">=</span><span class="token number">16000</span><span class="token punctuation">,</span>
    thinking<span class="token operator">=</span><span class="token punctuation">{</span>
        <span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"enabled"</span><span class="token punctuation">,</span>
        <span class="token string">"budget_tokens"</span><span class="token punctuation">:</span> <span class="token number">10000</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{</span>
        <span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
        <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"Design a microservices architecture for an e-commerce platform with 50k concurrent users."</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">)</span>

<span class="token keyword">for</span> block <span class="token keyword">in</span> response<span class="token punctuation">.</span>content<span class="token punctuation">:</span>
    <span class="token keyword">if</span> block<span class="token punctuation">.</span><span class="token builtin">type</span> <span class="token operator">==</span> <span class="token string">"thinking"</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"REASONING:"</span><span class="token punctuation">,</span> block<span class="token punctuation">.</span>thinking<span class="token punctuation">)</span>
    <span class="token keyword">elif</span> block<span class="token punctuation">.</span><span class="token builtin">type</span> <span class="token operator">==</span> <span class="token string">"text"</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"RESPONSE:"</span><span class="token punctuation">,</span> block<span class="token punctuation">.</span>text<span class="token punctuation">)</span></code></pre>
<p>The <code>budget_tokens</code> parameter controls how many tokens the thinking can use. More budget = more reasoning = better response (at higher cost and latency).</p>
<h2 id="when-to-use-extended-thinking" tabindex="-1">When to use extended thinking</h2>
<p><strong>Worth enabling for:</strong></p>
<ul>
<li>Complex software architecture problems</li>
<li>Code analysis with multiple dependencies</li>
<li>Mathematical or logical reasoning</li>
<li>Decisions with many trade-offs</li>
<li>Tasks where the first attempt is usually wrong</li>
</ul>
<p><strong>Not needed for:</strong></p>
<ul>
<li>Creative text generation</li>
<li>Translations</li>
<li>Structured data extraction</li>
<li>Questions with direct answers</li>
</ul>
<h2 id="recommended-budget-tokens-by-use-case" tabindex="-1">Recommended budget tokens by use case</h2>
<table>
<thead>
<tr>
<th>Task</th>
<th>Suggested budget</th>
</tr>
</thead>
<tbody>
<tr>
<td>Simple code analysis</td>
<td>2,000</td>
</tr>
<tr>
<td>Architecture design</td>
<td>8,000</td>
</tr>
<tr>
<td>Hard math problems</td>
<td>10,000</td>
</tr>
<tr>
<td>Complex systems analysis</td>
<td>15,000+</td>
</tr>
</tbody>
</table>
<p>The model stops when it reaches a satisfactory answer, not when it exhausts the budget. If you see thinking cutting off before concluding, increase the budget.</p>
<h2 id="streaming-with-extended-thinking" tabindex="-1">Streaming with extended thinking</h2>
<p>For production, use streaming to avoid blocking while Claude thinks:</p>
<pre class="language-python"><code class="language-python"><span class="token keyword">with</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>stream<span class="token punctuation">(</span>
    model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
    max_tokens<span class="token operator">=</span><span class="token number">16000</span><span class="token punctuation">,</span>
    thinking<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"enabled"</span><span class="token punctuation">,</span> <span class="token string">"budget_tokens"</span><span class="token punctuation">:</span> <span class="token number">8000</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"..."</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">)</span> <span class="token keyword">as</span> stream<span class="token punctuation">:</span>
    <span class="token keyword">for</span> event <span class="token keyword">in</span> stream<span class="token punctuation">:</span>
        <span class="token keyword">if</span> <span class="token builtin">hasattr</span><span class="token punctuation">(</span>event<span class="token punctuation">,</span> <span class="token string">'type'</span><span class="token punctuation">)</span> <span class="token keyword">and</span> event<span class="token punctuation">.</span><span class="token builtin">type</span> <span class="token operator">==</span> <span class="token string">'content_block_delta'</span><span class="token punctuation">:</span>
            <span class="token comment"># Process thinking or text based on block_index</span>
            <span class="token keyword">pass</span></code></pre>
<p>Extended thinking is one of Claude’s most underused capabilities. For problems where the first response isn’t enough, it’s the change that has the most impact on quality.</p>
]]></content>
  </entry>
  
  <entry>
    <title>Agent orchestration: patterns that scale</title>
    <link href="https://roigomez.com/en/blog/agent-orchestration-patterns/"/>
    <id>https://roigomez.com/en/blog/agent-orchestration-patterns/</id>
    <updated>2025-04-20</updated>
    <summary>How to design multi-agent systems with Claude that don&#39;t spiral out of control. Real coordination and handoff patterns.</summary>
    <content type="html"><![CDATA[<p>A single agent working alone is easy. A system where multiple agents coordinate work is where most people get lost. Three patterns solve 90% of cases.</p>
<h2 id="the-problem-with-scaling-agents" tabindex="-1">The problem with scaling agents</h2>
<p>A single agent with access to all tools and all context collapses quickly. The context window fills up, instructions mix together, and errors propagate uncontrolled. The solution isn’t bigger prompts — it’s smaller, well-coordinated systems.</p>
<h2 id="pattern-1%3A-orchestrator-%2B-workers" tabindex="-1">Pattern 1: Orchestrator + Workers</h2>
<p>The orchestrator receives the high-level task, breaks it down, and delegates to specialized workers. Each worker has a limited scope and specific tools.</p>
<pre><code>Orchestrator: &quot;Analyze this PR and write the report&quot;
  → Worker A: reviews code changes (tools: Read, Bash/git)
  → Worker B: verifies tests (tools: Bash/npm test)
  → Worker C: drafts report (tools: Write)
</code></pre>
<p>The orchestrator receives the outputs and assembles them. No worker needs to know what the others are doing.</p>
<h2 id="pattern-2%3A-sequential-pipeline" tabindex="-1">Pattern 2: Sequential pipeline</h2>
<p>For processes where each step depends on the previous one. Data flows from agent to agent, each transforming the previous agent’s output.</p>
<p>This pattern is simple but powerful for ETL processes, content pipelines, or any workflow where order matters. The risk is that an error in one step blocks the entire pipeline — you need to design handoffs with explicit validation.</p>
<h2 id="pattern-3%3A-parallel-fan-out" tabindex="-1">Pattern 3: Parallel fan-out</h2>
<p>When you have independent tasks that can run simultaneously. The orchestrator launches them in parallel and waits for all results before continuing.</p>
<p>The implementation with the Claude API is straightforward: multiple async calls with <code>asyncio.gather()</code> in Python or <code>Promise.all()</code> in JS. Each agent’s context is independent, so there’s no interference risk.</p>
<h2 id="the-practical-rule" tabindex="-1">The practical rule</h2>
<p>Start with one agent. When you notice the context window filling up or the agent mixing distinct concerns, that’s when to separate. Each split should have a concrete reason — no premature abstractions.</p>
]]></content>
  </entry>
  
  <entry>
    <title>The 5 prompting techniques that actually matter</title>
    <link href="https://roigomez.com/en/blog/5-prompting-techniques-that-matter/"/>
    <id>https://roigomez.com/en/blog/5-prompting-techniques-that-matter/</id>
    <updated>2025-04-14</updated>
    <summary>No empty theory. These are the techniques that changed results in real projects with Claude.</summary>
    <content type="html"><![CDATA[<p>There are dozens of “prompting techniques” floating around the internet. Most are noise. These five are what I use in production with measurable impact.</p>
<h2 id="1.-specific-role-%2B-context%2C-not-generic" tabindex="-1">1. Specific role + context, not generic</h2>
<p>“You are an expert in X” is too vague. A useful role includes specific domain context and the constraints that apply.</p>
<p>Instead of: <code>&quot;You are a TypeScript expert&quot;</code>, try: <code>&quot;You are a senior engineer reviewing TypeScript code for production. The codebase uses Node.js 20, strict mode, and Drizzle ORM. You prioritize security over brevity.&quot;</code> The difference in output quality is noticeable.</p>
<h2 id="2.-explicit-output-format" tabindex="-1">2. Explicit output format</h2>
<p>If you don’t say how you want the output, Claude will choose. Sometimes it chooses well; often it doesn’t. Specify: type (list, JSON, code, markdown), approximate length, and what to include/exclude.</p>
<p><code>&quot;Respond with a JSON array. Each object has: { error: string, severity: 'low'|'medium'|'high', fix: string }. No additional explanations.&quot;</code></p>
<h2 id="3.-chain-of-thought-for-complex-tasks" tabindex="-1">3. Chain-of-thought for complex tasks</h2>
<p>For complex reasoning, explicitly asking for the process improves output quality. <code>&quot;Think step by step before answering&quot;</code> activates a more careful reasoning mode. It’s especially useful in debugging and code analysis where the first impulse is often wrong.</p>
<h2 id="4.-few-shot-examples-for-exact-format" tabindex="-1">4. Few-shot examples for exact format</h2>
<p>When format matters (and it almost always does), one example is worth a thousand description words. Show input → expected output before giving the real task. With two or three examples, Claude replicates the pattern with high fidelity.</p>
<h2 id="5.-explicit-negative-constraints" tabindex="-1">5. Explicit negative constraints</h2>
<p>“Don’t do X” is as important as “do Y”. Negative constraints prevent default behaviors you don’t want: <code>&quot;Don't explain the code you generate&quot;</code>, <code>&quot;Don't use comments&quot;</code>, <code>&quot;Don't add unnecessary error handling&quot;</code>.</p>
<p>Without explicit constraints, Claude tends to be “helpful” by adding things you didn’t ask for. In an automated pipeline that’s noise, not value.</p>
]]></content>
  </entry>
  
  <entry>
    <title>My Claude Code workflow after 3 months</title>
    <link href="https://roigomez.com/en/blog/claude-code-workflow-3-months/"/>
    <id>https://roigomez.com/en/blog/claude-code-workflow-3-months/</id>
    <updated>2025-04-08</updated>
    <summary>What I changed, what I kept, and what I learned using Claude Code as my primary development tool.</summary>
    <content type="html"><![CDATA[<p>I’ve been using Claude Code as my primary tool for three months. Not as an occasional assistant — as part of the daily workflow. Here’s what I learned.</p>
<h2 id="what-changed-from-the-first-month" tabindex="-1">What changed from the first month</h2>
<p>The first month I used it like a powerful autocomplete. I’d ask for code, review it, adjust it. Reasonable productivity but not spectacular.</p>
<p>The change came when I started working with tasks, not questions. Instead of “write a function that does X,” I started describing the full objective: “implement the <code>/users/:id</code> endpoint with permission validation, standard error handling, and tests.” The difference is substantial — Claude can maintain the context of a complete task much better than fragmented questions.</p>
<h2 id="what-i-kept%3A-claude.md-as-memory" tabindex="-1">What I kept: <a href="http://CLAUDE.md">CLAUDE.md</a> as memory</h2>
<p>The <code>CLAUDE.md</code> file in the project root is the most valuable thing I adopted. It includes:</p>
<ul>
<li>Exact project stack (versions, libraries)</li>
<li>Code conventions (naming, folder structure)</li>
<li>Domain-specific rules (“no <code>any</code> in TypeScript”, “errors go in <code>{ code, message }</code> format”)</li>
<li>Business context Claude can’t infer from code</li>
</ul>
<p>Without this, every session started from scratch. With it, Claude has the context it needs to make correct decisions without asking me every time.</p>
<h2 id="what-i-discovered%3A-hooks-are-powerful" tabindex="-1">What I discovered: hooks are powerful</h2>
<p>Claude Code hooks (pre-tool, post-tool) let you automate actions around tools. I use them to: log which files get modified, run the linter automatically after each Write, and alert if it tries to modify files outside the allowed scope.</p>
<p>This turns Claude Code from “agent that executes what you ask” to “agent with automatic guardrails.”</p>
<h2 id="the-real-limit%3A-context-window" tabindex="-1">The real limit: context window</h2>
<p>After three months, the main limit isn’t response quality — it’s the context window. In large projects, the context fills up and quality drops. The solution isn’t to force more context, it’s to work in shorter sessions with more specific tasks. One session = one well-defined task.</p>
]]></content>
  </entry>
  
  <entry>
    <title>Hooks in Claude Code: automate guardrails with shell commands</title>
    <link href="https://roigomez.com/en/blog/hooks-claude-code/"/>
    <id>https://roigomez.com/en/blog/hooks-claude-code/</id>
    <updated>2025-04-08</updated>
    <summary>How to use hooks in Claude Code to run commands before or after each agent action. Full control over what Claude does in your project.</summary>
    <content type="html"><![CDATA[<p>Hooks are one of Claude Code’s least-known and most powerful features. They let you run shell commands automatically before or after Claude uses any tool. Guardrails without friction.</p>
<h2 id="what-hooks-are-and-what-they%E2%80%99re-for" tabindex="-1">What hooks are and what they’re for</h2>
<p>A hook is a shell command that Claude Code runs in response to agent events. Available events:</p>
<ul>
<li><code>PreToolUse</code> — before Claude uses a tool</li>
<li><code>PostToolUse</code> — after Claude uses a tool</li>
<li><code>Notification</code> — when Claude wants to notify you of something</li>
<li><code>Stop</code> — when Claude finishes a task</li>
</ul>
<p>Real use cases: run the linter after each edit, take a git snapshot before destructive changes, block edits on protected files, send Slack notifications when the agent finishes.</p>
<h2 id="configuring-hooks-in-settings.json" tabindex="-1">Configuring hooks in settings.json</h2>
<p>Hooks are configured in <code>~/.claude/settings.json</code> (global) or <code>.claude/settings.json</code> (project):</p>
<pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"PostToolUse"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        <span class="token property">"matcher"</span><span class="token operator">:</span> <span class="token string">"Write|Edit"</span><span class="token punctuation">,</span>
        <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
          <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"command"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"cd $CLAUDE_PROJECT_DIR &amp;&amp; npx eslint --fix $CLAUDE_FILE_PATHS 2>/dev/null || true"</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>The <code>matcher</code> is a regex matching the tool name. <code>Write|Edit</code> captures both editing tools.</p>
<h2 id="available-environment-variables" tabindex="-1">Available environment variables</h2>
<p>Claude Code exposes useful variables in each hook:</p>
<ul>
<li><code>$CLAUDE_PROJECT_DIR</code> — project root directory</li>
<li><code>$CLAUDE_FILE_PATHS</code> — files affected by the tool (space-separated)</li>
<li><code>$CLAUDE_TOOL_NAME</code> — name of the tool that ran</li>
<li><code>$CLAUDE_SESSION_ID</code> — current session ID</li>
</ul>
<h2 id="blocking-protected-files" tabindex="-1">Blocking protected files</h2>
<p>You can use the exit code to block actions. If the hook returns exit code 2, Claude Code cancels the action and shows stderr as an error message:</p>
<pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"PreToolUse"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        <span class="token property">"matcher"</span><span class="token operator">:</span> <span class="token string">"Write|Edit"</span><span class="token punctuation">,</span>
        <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
          <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"command"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"if echo \"$CLAUDE_FILE_PATHS\" | grep -q 'production.env\\|\\.secrets'; then echo 'Protected file: edit blocked' >&amp;2; exit 2; fi"</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<h2 id="auto-commit-hook-after-each-task" tabindex="-1">Auto-commit hook after each task</h2>
<p>A useful pattern: auto-commit when Claude finishes, to have a granular history of each agent change:</p>
<pre class="language-json"><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token property">"Stop"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        <span class="token property">"matcher"</span><span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">,</span>
        <span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
          <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"command"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"cd $CLAUDE_PROJECT_DIR &amp;&amp; git diff --quiet || git add -A &amp;&amp; git commit -m 'chore: claude agent checkpoint'"</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
<p>Hooks turn Claude Code into an auditable, controlled agent. With a few shell commands you define exactly what it can and cannot do in your project.</p>
]]></content>
  </entry>
  
  <entry>
    <title>Tool use in Claude API: practical guide with real examples</title>
    <link href="https://roigomez.com/en/blog/tool-use-claude-api-guide/"/>
    <id>https://roigomez.com/en/blog/tool-use-claude-api-guide/</id>
    <updated>2025-03-25</updated>
    <summary>How to implement function calling in Claude API. From tool definition to handling responses in production.</summary>
    <content type="html"><![CDATA[<p>Tool use (function calling) is the capability that transforms Claude from a text generator into an agent that can interact with the real world. With tools, Claude can query APIs, read databases, run calculations, or call any function you define.</p>
<h2 id="how-the-tool-use-cycle-works" tabindex="-1">How the tool use cycle works</h2>
<p>The flow is a 3-step cycle:</p>
<ol>
<li>You send Claude a list of available tools with their descriptions and schemas</li>
<li>Claude decides whether it needs a tool and returns a <code>tool_use</code> block with the parameters</li>
<li>You execute the tool, return the result, and Claude generates the final response</li>
</ol>
<pre class="language-python"><code class="language-python"><span class="token keyword">import</span> anthropic

client <span class="token operator">=</span> anthropic<span class="token punctuation">.</span>Anthropic<span class="token punctuation">(</span><span class="token punctuation">)</span>

tools <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token string">"name"</span><span class="token punctuation">:</span> <span class="token string">"get_weather"</span><span class="token punctuation">,</span>
        <span class="token string">"description"</span><span class="token punctuation">:</span> <span class="token string">"Gets the current weather for a city"</span><span class="token punctuation">,</span>
        <span class="token string">"input_schema"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"object"</span><span class="token punctuation">,</span>
            <span class="token string">"properties"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
                <span class="token string">"city"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"string"</span><span class="token punctuation">,</span> <span class="token string">"description"</span><span class="token punctuation">:</span> <span class="token string">"City name"</span><span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token string">"required"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">"city"</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span>

response <span class="token operator">=</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>create<span class="token punctuation">(</span>
    model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
    max_tokens<span class="token operator">=</span><span class="token number">1024</span><span class="token punctuation">,</span>
    tools<span class="token operator">=</span>tools<span class="token punctuation">,</span>
    messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"What's the weather like in London?"</span><span class="token punctuation">}</span><span class="token punctuation">]</span>
<span class="token punctuation">)</span></code></pre>
<h2 id="processing-the-tool-use-response" tabindex="-1">Processing the tool use response</h2>
<p>Claude can return <code>tool_use</code> in the <code>stop_reason</code>. You need to handle this case:</p>
<pre class="language-python"><code class="language-python"><span class="token keyword">if</span> response<span class="token punctuation">.</span>stop_reason <span class="token operator">==</span> <span class="token string">"tool_use"</span><span class="token punctuation">:</span>
    tool_block <span class="token operator">=</span> <span class="token builtin">next</span><span class="token punctuation">(</span>b <span class="token keyword">for</span> b <span class="token keyword">in</span> response<span class="token punctuation">.</span>content <span class="token keyword">if</span> b<span class="token punctuation">.</span><span class="token builtin">type</span> <span class="token operator">==</span> <span class="token string">"tool_use"</span><span class="token punctuation">)</span>
    tool_name <span class="token operator">=</span> tool_block<span class="token punctuation">.</span>name
    tool_input <span class="token operator">=</span> tool_block<span class="token punctuation">.</span><span class="token builtin">input</span>

    <span class="token comment"># Execute your real function here</span>
    result <span class="token operator">=</span> call_your_function<span class="token punctuation">(</span>tool_name<span class="token punctuation">,</span> tool_input<span class="token punctuation">)</span>

    <span class="token comment"># Return the result to Claude</span>
    final_response <span class="token operator">=</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>create<span class="token punctuation">(</span>
        model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
        max_tokens<span class="token operator">=</span><span class="token number">1024</span><span class="token punctuation">,</span>
        tools<span class="token operator">=</span>tools<span class="token punctuation">,</span>
        messages<span class="token operator">=</span><span class="token punctuation">[</span>
            <span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token string">"What's the weather like in London?"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"assistant"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> response<span class="token punctuation">.</span>content<span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span><span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
                <span class="token punctuation">{</span><span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"tool_result"</span><span class="token punctuation">,</span> <span class="token string">"tool_use_id"</span><span class="token punctuation">:</span> tool_block<span class="token punctuation">.</span><span class="token builtin">id</span><span class="token punctuation">,</span> <span class="token string">"content"</span><span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">}</span>
            <span class="token punctuation">]</span><span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">)</span></code></pre>
<h2 id="multiple-tools-and-tool-choice" tabindex="-1">Multiple tools and tool choice</h2>
<p>You can define several tools and Claude will choose the appropriate one. With <code>tool_choice</code> you can force the use of a specific tool or disable automatic selection.</p>
<pre class="language-python"><code class="language-python"><span class="token comment"># Force a specific tool</span>
response <span class="token operator">=</span> client<span class="token punctuation">.</span>messages<span class="token punctuation">.</span>create<span class="token punctuation">(</span>
    model<span class="token operator">=</span><span class="token string">"claude-sonnet-4-6"</span><span class="token punctuation">,</span>
    max_tokens<span class="token operator">=</span><span class="token number">1024</span><span class="token punctuation">,</span>
    tools<span class="token operator">=</span>tools<span class="token punctuation">,</span>
    tool_choice<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">"type"</span><span class="token punctuation">:</span> <span class="token string">"tool"</span><span class="token punctuation">,</span> <span class="token string">"name"</span><span class="token punctuation">:</span> <span class="token string">"get_weather"</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
    messages<span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">]</span>
<span class="token punctuation">)</span></code></pre>
<h2 id="when-to-use-tool-use-vs-plain-prompt" tabindex="-1">When to use tool use vs plain prompt</h2>
<p>Tool use adds latency and complexity. Use it when you need real-time external data, executing actions with side effects, or when the result depends on information not in context. For pure reasoning or text generation, a direct prompt is faster and cheaper.</p>
]]></content>
  </entry>
  
</feed>
