Code Examples
Use anytool with any LLM framework or build your own integration
Vercel AI SDK (TypeScript)
The AI SDK provides a unified interface for working with LLMs. This is the recommended approach for TypeScript/JavaScript apps.
import { generateText, tool } from 'ai'
import { openai } from '@ai-sdk/openai'
import { z } from 'zod'
const anytool = tool({
description: 'Generate and execute any tool on demand',
parameters: z.object({
prompt: z.string().describe('What tool to generate'),
input: z.string().describe('Input for the tool')
}),
execute: async ({ prompt, input }) => {
const res = await fetch('https://anytoolhq.com/api/tool', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.ANYTOOL_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ prompt, input })
})
const data = await res.json()
return data.output
}
})
// Example usage
const result = await generateText({
model: openai('gpt-5'),
tools: { anytool },
prompt: 'Create a QR code for https://example.com and show it to me'
})
console.log(result.text)LangChain (Python)
Use anytool with LangChain's tool calling capabilities:
import os
import requests
from langchain.tools import Tool
from langchain.agents import initialize_agent, AgentType
from langchain_openai import ChatOpenAI
def anytool_execute(query: str) -> str:
"""Execute anytool with a prompt and optional input.
Format: 'prompt|input' or just 'prompt'
"""
parts = query.split('|', 1)
prompt = parts[0].strip()
input_data = parts[1].strip() if len(parts) > 1 else ""
response = requests.post(
'https://anytoolhq.com/api/tool',
headers={
'Authorization': f"Bearer {os.environ['ANYTOOL_API_KEY']}",
'Content-Type': 'application/json'
},
json={'prompt': prompt, 'input': input_data}
)
if response.ok:
return response.json()['output']
else:
return f"Error: {response.text}"
anytool = Tool(
name="anytool",
description="Generate and execute any tool on demand. "
"Use format 'tool description|input data'",
func=anytool_execute
)
llm = ChatOpenAI(model="gpt-4")
agent = initialize_agent(
[anytool],
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True
)
# Example usage
result = agent.run("Create a QR code for https://example.com")
print(result)Custom Implementation (Node.js)
If you're building your own LLM integration, here's a minimal example:
async function callAnytool({ prompt, input }) {
const response = await fetch('https://anytoolhq.com/api/tool', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.ANYTOOL_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt, // What the tool should do
input // Data to process
})
})
if (!response.ok) {
throw new Error(`API error: ${response.statusText}`)
}
const result = await response.json()
return {
output: result.output, // The actual result
outputType: result.outputType, // 'text', 'json', 'html', 'svg', etc.
contentType: result.contentType, // MIME type
cached: result.cached // Was it cached or generated?
}
}
// Example: QR Code
const qrResult = await callAnytool({
prompt: 'QR code generator',
input: 'https://example.com'
})
console.log(qrResult.output) // SVG or image data
// Example: Temperature conversion
const tempResult = await callAnytool({
prompt: 'Temperature converter',
input: '100F to C'
})
console.log(tempResult.output) // "37.78°C"Custom Implementation (Python)
Minimal Python implementation without LangChain:
import os
import requests
def call_anytool(prompt: str, input_data: str = "") -> dict:
"""Call anytool API."""
response = requests.post(
'https://anytoolhq.com/api/tool',
headers={
'Authorization': f"Bearer {os.environ['ANYTOOL_API_KEY']}",
'Content-Type': 'application/json'
},
json={
'prompt': prompt,
'input': input_data
}
)
response.raise_for_status()
return response.json()
# Example usage
result = call_anytool(
prompt='QR code generator',
input_data='https://example.com'
)
print(f"Output: {result['output']}")
print(f"Type: {result['outputType']}")
print(f"Cached: {result['cached']}")Tool Description Best Practices
When your LLM calls anytool, it needs to provide a clear prompt. Here are tips:
Good Prompts
"QR code generator""Convert temperature between Celsius and Fahrenheit""Generate secure password with configurable length"Less Effective Prompts
"Do something with this URL" (too vague)"Tool" (no description)API Response Format
The API returns a consistent response format:
{
"output": "...", // The actual result (string)
"outputType": "svg", // Type: text, json, html, svg, image, csv, xml
"contentType": "image/svg+xml", // MIME type
"cached": true, // Whether result was cached
"toolId": "abc123" // Tool identifier (for tracking)
}