Tutorial

How to Add Search Tool to Pi Coding Agent

Give Pi coding agent web search capability via Scavio API. Tool registration, search function, and test workflow.

Pi coding agent works well for code generation but lacks web access for looking up docs, APIs, and current best practices. Adding a search tool lets Pi verify package versions, find documentation, and check for recent breaking changes before writing code. Each lookup costs $0.005.

Prerequisites

  • Pi coding agent installed
  • Python 3.8+
  • A Scavio API key from scavio.dev
  • Basic Pi agent configuration knowledge

Walkthrough

Step 1: Create search tool for Pi agent

Build the search function that Pi can call during code generation.

Python
import os, requests, json

API_KEY = os.environ['SCAVIO_API_KEY']
SH = {'x-api-key': API_KEY, 'Content-Type': 'application/json'}

def pi_web_search(query, num_results=5):
    """Web search tool for Pi coding agent.
    Use to look up current documentation, package versions,
    API references, and recent breaking changes."""
    data = requests.post('https://api.scavio.dev/api/v1/search',
        headers=SH, json={'query': query, 'country_code': 'us', 'num_results': num_results}).json()
    results = []
    for r in data.get('organic_results', [])[:num_results]:
        results.append({'title': r.get('title', ''), 'url': r.get('link', ''),
                        'snippet': r.get('snippet', '')})
    return results

def pi_extract_page(url):
    """Extract content from a documentation page.
    Use after search to read full API docs or tutorials."""
    data = requests.post('https://api.scavio.dev/api/v1/extract',
        headers=SH, json={'url': url}).json()
    return data.get('content', '')[:3000]

# Pi tool registration format
tools = [
    {'name': 'web_search', 'fn': pi_web_search,
     'desc': 'Search web for docs, versions, APIs. Returns title, url, snippet.'},
    {'name': 'read_page', 'fn': pi_extract_page,
     'desc': 'Read content from a URL. Use for documentation pages.'}
]
print(f'Registered {len(tools)} tools for Pi agent')
for t in tools:
    print(f'  {t["name"]}: {t["desc"]}')

Step 2: Configure Pi agent to use search

Add the search tool to Pi agent configuration.

Python
# Pi agent config: ~/.pi/tools.json
pi_config = {
    'tools': [
        {
            'name': 'web_search',
            'description': 'Search the web for documentation, package versions, and API references. Use before recommending specific versions or APIs.',
            'input_schema': {
                'type': 'object',
                'properties': {
                    'query': {'type': 'string', 'description': 'What to search for'}
                },
                'required': ['query']
            }
        },
        {
            'name': 'read_page',
            'description': 'Read full content from a documentation URL found via search.',
            'input_schema': {
                'type': 'object',
                'properties': {
                    'url': {'type': 'string', 'description': 'URL to read'}
                },
                'required': ['url']
            }
        }
    ]
}

print('Pi agent tool config:')
print(json.dumps(pi_config, indent=2))
print(f'\nSave to ~/.pi/tools.json and restart Pi agent.')

Step 3: Test Pi agent with search-dependent tasks

Verify the agent uses search for version checks and API lookups.

Python
def simulate_pi_workflow(task):
    """Simulate Pi agent using search during code generation."""
    print(f'\n=== Pi Agent: "{task}" ===')
    # Step 1: Search for current info
    search_query = task.replace('write code to ', '').replace('how to ', '')
    results = pi_web_search(f'{search_query} python 2026')
    print(f'  [Search] {len(results)} results for: {search_query}')
    # Step 2: Read top doc
    if results and results[0]['url']:
        print(f'  [Read] {results[0]["title"][:50]}')
        print(f'  [Snippet] {results[0]["snippet"][:80]}')
    # Step 3: Agent generates code with correct info
    print(f'  [Generate] Code generated with verified versions and APIs')
    print(f'  [Cost] $0.005 per search')

tasks = [
    'write code to connect to PostgreSQL with latest asyncpg',
    'how to use FastAPI 0.115 WebSocket middleware',
    'create a Pydantic v2 model with computed fields',
]
for task in tasks:
    simulate_pi_workflow(task)
print(f'\nTotal cost: ${len(tasks) * 0.005:.3f}')

Python Example

Python
import os, requests
SH = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}

def pi_search(query):
    data = requests.post('https://api.scavio.dev/api/v1/search',
        headers=SH, json={'query': query, 'country_code': 'us', 'num_results': 3}).json()
    for r in data.get('organic_results', [])[:3]:
        print(f'  {r["title"]}: {r.get("snippet", "")[:60]}')

pi_search('fastapi latest version 2026')
print('Cost: $0.005')

JavaScript Example

JavaScript
const SH = { 'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json' };
const data = await fetch('https://api.scavio.dev/api/v1/search', {
  method: 'POST', headers: SH,
  body: JSON.stringify({ query: 'fastapi latest version 2026', country_code: 'us', num_results: 3 })
}).then(r => r.json());
(data.organic_results || []).slice(0, 3).forEach(r => console.log(r.title));

Expected Output

JSON
Registered 2 tools for Pi agent
  web_search: Search web for docs, versions, APIs. Returns title, url, snippet.
  read_page: Read content from a URL. Use for documentation pages.

=== Pi Agent: "write code to connect to PostgreSQL with latest asyncpg" ===
  [Search] 5 results for: connect to PostgreSQL with latest asyncpg
  [Read] asyncpg Documentation - Connection Pool Setup
  [Generate] Code generated with verified versions and APIs
  [Cost] $0.005 per search

Total cost: $0.015

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Pi coding agent installed. Python 3.8+. A Scavio API key from scavio.dev. Basic Pi agent configuration knowledge. A Scavio API key gives you 250 free credits per month.

Yes. The free tier includes 250 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

Give Pi coding agent web search capability via Scavio API. Tool registration, search function, and test workflow.