Building AI Endpoints
By the end you have a POST endpoint that takes a text, asks Claude and replies with clean JSON, with spend capped. From there: streaming, classifying, tools and bulk batches.
1. Every Claude block takes a connection
Create it once per flow with function_create_anthropicconnection (open connection).
| Pin | Set it to |
|---|---|
api_key | Your Anthropic key. Keep it off the canvas: store it as a system setting and read it with value_get_system_key_select (get setting). |
default_model | The model every message uses unless the block overrides it. |
daily_cap_usd, concurrency_cap | Hard guardrails on spend and parallel calls, enforced by the connection itself. |
The block has a success condition pin and an error value pin. Branch on them: a message sent without a working connection fails further down the flow.
2. A request goes in, an answer comes back as JSON
The dominant API shape in RUAL blueprints works for AI too: a trigger runs the chain, the connection and prompt feed the message block, and a JSON reply answers the caller. The prompt here is the text field of the request body, read with httpconnection_get_body (get body) and object_field_getter_multiple (get fields).
The standard AI endpoint shape: a function trigger runs the chain, open connection creates the Anthropic connection, and send message asks Claude with the request body text as the prompt, read off the body by get fields. An output template on the output_schema pin keeps the answer valid JSON, and reply in JSON returns it to the caller with a 200.
On the canvas: anthropicconnection_message (send message) takes the connection, the prompt and an optional system text, and outputs the reply as text plus token usage and a stop_reason. httpconnection_set_json (reply in JSON) sends the answer back with status 200. Always branch on the message block's success pin and reply with an error body on the false path, following Error Handling.
3. Two blocks turn the reply into dependable data
Free text is hard to process further.
anthropicconnection_build_schema(build output template): pick the fields Claude should return and describe each one, no JSON Schema knowledge needed. Wire its output to the message block'soutput_schemapin and the reply text is valid JSON in that shape.anthropicconnection_parse_json(parse JSON (repair)): recovers and parses JSON from messy text (code fences, prose around the object, truncation) without an API call. It reportsrepairedwhen it had to fix the input, so you can log how often that happens.
With an output template wired, the text pin already parses cleanly. Keep the parse block as the safety net before you feed the reply into object blocks or storage.
How you know it worked: post a text to your endpoint with curl. The answer is JSON with exactly your fields.
4. Streaming pushes the reply token by token
anthropicconnection_message_stream (stream message) sends the same prompt but pushes the reply to a WebSocket channel while it generates, then returns the full text when done. Use it for chat-like screens where waiting for the whole answer feels broken; use the plain message block everywhere else, it is simpler to debug. Web pages open no WebSocket by themselves, so a page that shows a stream needs a client of its own in a script.
5. Classify sorts input into your labels
anthropicconnection_classify (classify) sorts an input into exactly one of your labels, with a matched condition for "none of them fit". Feed the label into a value_switch (switch) to route support tickets, form submissions or webhook payloads down the right branch without writing conditions by hand.
6. Tools let Claude call your functions
Tools let Claude call back into your blueprint while it composes an answer.
Build a tool
anthropicconnection_build_tool(build tool): name, description, parameters as a schema, and the function that runs when Claude calls it.Combine them
anthropicconnection_tool_multiple(multiple) combines tools.anthropicconnection_tool_branch(tool if) andanthropicconnection_tool_switch(tool switch) pick one conditionally.Wire them in
Connect the result to the message block's
toolspin. Themax_iterationspin caps how many tool round trips one message may make.
Ready-made tools cover web search (anthropicconnection_tool_web_search) and code execution (anthropicconnection_tool_code_execution).
7. Four ways to control cost
anthropicconnection_count_tokens(count tokens) prices a prompt before you send it: input tokens, estimated total cost, free to call. Gate expensive calls on it.anthropicconnection_preflight(preflight document) converts a PDF, image or spreadsheet to clean Markdown with a cheap model, so the expensive model reads far fewer tokens.anthropicconnection_preflight_batchdoes the same for a list of files.- The message block's
cachepin reuses earlier context instead of paying for it again, andanthropicconnection_get_usage(get usage) reports total spend per connection. - The connection's daily cap is the last line of defense. Set it on every production blueprint.
8. Bulk work runs as batches at half price
anthropicconnection_batch_create (create batch) submits many prompts as one asynchronous batch at half the standard price, with results within 24 hours. Wire its on_finished function pin to be called when the batch completes (the system polls in the background), or poll yourself with anthropicconnection_batch_get (get batch) from a scheduled flow. Batches are the right shape for nightly enrichment, classification backfills and report generation. Anything the user waits for belongs in a plain message call.
Notes from practice
- Long AI calls belong behind the queue: hand the work to
function_custom_execute_from_queue(execute in queue) and reply to the caller immediately. See Queue. - Expose AI endpoints with a scope and a rate limit; an unprotected
/api/askis an open wallet. See Remote Access Control. - Conversations that remember earlier turns use the memory blocks (
anthropicconnection_create_memoryand friends) on the message block'smemorypin.
Next steps
Anthropic Claude Every Claude block in one place. Common Blueprint Patterns The endpoint, validation and error-handling shapes this tutorial builds on. Remote Access Control Scopes and rate limits for your AI endpoint. Block Templates Copyable block combinations, including the API endpoint shape.Frequently asked
How do I call Claude from a RUAL blueprint?
Create a connection with function_create_anthropicconnection using your Anthropic API key, then wire that connection to anthropicconnection_message with your prompt. The message block returns the reply text, token usage and a success condition to branch on.
How do I get structured JSON from Claude in RUAL?
Build an output template with anthropicconnection_build_schema, describing each field Claude should return, and wire it to the message block's output_schema pin. The reply text is then valid JSON in that shape, and anthropicconnection_parse_json repairs any malformed output without another API call.
How do I control what Claude costs in a RUAL blueprint?
Set a daily_cap_usd on the Anthropic connection as a hard limit, price a prompt before sending it with anthropicconnection_count_tokens, and convert documents to Markdown with the cheaper preflight block before sending them to an expensive model. Bulk work goes through anthropicconnection_batch_create at half the standard price.
