LLMs

These examples show an agent asking a language model for text or JSON, with the host deciding which model the agent gets and how many times it may ask.

Each example has two parts. The agent is AJS, and could have been written by a model or a stranger. The host is your JavaScript: it creates the VM and passes in a model to call. The VM has no IO of its own, so an agent cannot reach a model unless the host passes one in as the llm capability. Here is the whole agent side of a model call:

function ask(question: 'What is the capital of France?') {
  let answer = llmPredict({ prompt: question })
  return { answer }
}

What an agent may spend

A model call is where an untrusted agent could run up your bill, so the host limits it three ways:

All three are vm.run options, described in Embedding the AJS VM.

Running the examples

This site runs in your browser with no model server behind it. On these pages, the examples that call a model print a notice instead of a result: "This example needs an LLM, which the static site does not provide." That notice is expected, not a failure. Examples that make no model call still run.

To run them yourself you need:

Start with LLM Chat. It shows the host code once: transpiling the agent, an llm object whose predict posts to the server, and the vm.run call that passes it in. That host posts to LM Studio's default address (http://localhost:1234); for MLX or another server, change the URL in it. The other two pages show only the agent and tell you to use that same llm object, so copy it from LLM Chat.

What each one teaches:

More LLM examples are in other sections. Tool calls, vision and code generation are in Advanced (LLM with Tool, Vision: OCR). Combining a model with an API call is in Featured (LLM + API Data).