% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/llama.R
\name{llama_chat_build}
\alias{llama_chat_build}
\title{Build a tool-aware chat prompt and its parsing grammar}
\usage{
llama_chat_build(
  model,
  messages,
  tools = NULL,
  tool_choice = NULL,
  json_schema = NULL,
  add_generation_prompt = TRUE,
  enable_thinking = TRUE
)
}
\arguments{
\item{model}{A model handle from \code{\link{llama_load_model}}.}

\item{messages}{List of messages in OpenAI chat shape: each a list with
\code{role} and \code{content} (content may be a string or a list of
content parts). Assistant tool calls and tool results are supported via
the usual \code{tool_calls} / \code{tool_call_id} fields.}

\item{tools}{Optional list of tool definitions in OpenAI shape (each a list
with \code{type = "function"} and a \code{function} entry holding
\code{name}, \code{description}, \code{parameters}). \code{NULL} for a
plain chat prompt.}

\item{tool_choice}{One of \code{"auto"}, \code{"required"}, \code{"none"},
or \code{NULL} to leave it at the template default.}

\item{json_schema}{Optional JSON-schema string to constrain free-form output
(structured output). Mutually meaningful with \code{tools = NULL}.}

\item{add_generation_prompt}{Whether to append the assistant prompt prefix.}

\item{enable_thinking}{Whether to allow reasoning blocks for models that
support them.}
}
\value{
A list with elements \code{prompt}, \code{grammar} (possibly empty),
  \code{format} (integer format id for \code{\link{llama_chat_parse}}),
  \code{grammar_lazy}, \code{additional_stops}, and \code{preserved_tokens}.
}
\description{
Applies the model's built-in chat template (via llama.cpp's Jinja path,
i.e. \code{common_chat_templates_apply}) to a conversation that may include
tool definitions, returning both the formatted \code{prompt} and the
\code{grammar} that constrains the model to emit tool calls in the format
that template expects. Unlike \code{\link{llama_chat_apply_template}} (which
uses the low-level template path and is text-only), this understands
\code{tools} and is what \code{\link{llama_serve_anthropic}} uses to support
tool calling.
}
\details{
Pair the returned \code{format} with \code{\link{llama_chat_parse}} to turn
the model's raw output back into content and structured tool calls.
}
\seealso{
[llama_chat_parse], [llama_serve_anthropic]
}
