Web search and page reading

Your app searches the web and reads pages with the key it already has. playcode.web.search() answers the best results for a query, playcode.web.fetch() answers a page's main text, and playcode.web.tools() gives an agent both. Each call is paid from the workspace's credits. For the whole path, from the backend service to a daily limit, see Search the web from your app.

On this page

Search the web

#
const { results, usage } = await playcode.web.search({
  query: 'electric bike rental prices in Lisbon',
  maxResults: 5,
})
for (const result of results) {
  console.log(result.title, result.url, result.snippet)
}
// usage.credits is what the search cost
Field What it does
query What to look for, as a person types it into a search engine
maxResults How many results, 1 to 10. Default 5
includeDomains Only results from these sites: ['docs.stripe.com']
excludeDomains No results from these sites

The results come best first. Each has title, url, snippet (a short passage that matches the query), score (0 to 1) and publishedDate. score and publishedDate are null when the search provider does not give them.

Read a page

#
const page = await playcode.web.fetch({ url: 'https://example.com/pricing' })
// page.content is the page's main text as Markdown
  • format: 'html' answers the page's HTML instead of Markdown.
  • finalUrl is the address that answered, after redirects. title, byline and excerpt are null when the page has none.
  • A very long page comes back cut, with truncated: true.
  • Only a public http or https address can be read. Playcode refuses a private, local or unknown address with 400 validation, and nobody reads it.
  • A page that Playcode's reader cannot read, such as a PDF, goes to a fallback reader at a higher price. via says which one read it: own or extract.

Give an agent the web

#
const researcher = playcode.ai.agent({
  model,
  system: 'You answer questions about our market. Name the pages you used.',
  tools: { ...playcode.web.tools(), saveNote },
  budget: { credits: 1 },
})

const { text } = await researcher.run('What do bike rentals cost in Lisbon this year?')

webSearch searches, and webFetch reads one page as Markdown. Their descriptions tell the model when to use each. The model reads the first 30,000 characters of a page, and a note says when the page was longer.

Everything these tools return was written outside your app, and a page can hold text meant to steer the model. Each title, address, snippet and page reaches the model inside a marked data block, so the model reads it as data, not as instructions. When your own code calls fetch() and puts the text into a prompt, it is not marked: see Boundaries.

When Playcode refuses a call, the model reads the refusal as the tool's error, with what to do next, and the run goes on: the credits ran out, a limit was reached, or the address is not public.

What a call costs

#
Call Credits
A search 0.0912
A page that Playcode's reader reads 0.0064
A page that the fallback reader reads 0.01824
An answer from the cache: the same search within 5 minutes, or the same page within 15 minutes Free

Each answer's usage says what the call cost: credits, the units it charged (search, read or page), and its id, which support can look up. cached: true marks an answer from the cache.

Errors

#

A refusal is a PlaycodeApiError with Playcode's own message. Read status and code to decide what to do.

Status and code What it means
402 insufficient_balance The workspace's credits do not cover the call. remainingCredits says what is left
400 validation An empty query, a value out of range, or an address that is not public. The message names the field
403 missing_scope The app's key does not have the web service
429 rate_limited Too many calls this minute. retryAfterMs says when the next one can pass
429 daily_cap The app's daily web search limit is spent, until 00:00 UTC. The owner raises it in Settings, AI
502 upstream, 503 unavailable, 504 timeout The web service or its provider failed. Try again later
import { PlaycodeApiError } from '@playcode/sdk'

try {
  await playcode.web.search({ query: 'bike rentals Lisbon' })
} catch (error) {
  if (error instanceof PlaycodeApiError && error.code === 'daily_cap') {
    // Show what you already have; the limit resets at 00:00 UTC.
  } else {
    throw error
  }
}

PlaycodeConnectionError means the call did not reach Playcode. A call waits 65 seconds by default (timeoutMs), and the SDK never retries it by itself.

Limits

#
  • An app makes at most 60 web calls a minute.
  • An app spends at most 20 credits a day on web calls by default, until 00:00 UTC. The owner sets the app's own limit in the project's Settings, AI, or asks the agent.
  • The app's key needs the web service. A new Cloud app has it. For an older app, ask Playcode's agent to switch on web for the app.
  • The address is PLAYCODE_WEB_URL, which a Cloud app has in its environment. Without it, the SDK uses the AI address (PLAYCODE_AI_URL). webUrl in the client's options overrides both.
  • The search tools that a model's maker runs itself stay refused (server_tool_not_supported). Give the agent playcode.web.tools() instead.