% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/llama.R
\name{llama_gen_begin}
\alias{llama_gen_begin}
\title{Begin a streaming (token-by-token) generation}
\usage{
llama_gen_begin(
  ctx,
  prompt,
  max_new_tokens = 256L,
  temp = 0.8,
  top_k = 50L,
  top_p = 0.9,
  seed = 42L,
  min_p = 0,
  typical_p = 1,
  repeat_penalty = 1,
  repeat_last_n = 64L,
  frequency_penalty = 0,
  presence_penalty = 0,
  mirostat = 0L,
  mirostat_tau = 5,
  mirostat_eta = 0.1,
  grammar = NULL,
  trigger_patterns = NULL,
  trigger_tokens = NULL
)
}
\arguments{
\item{ctx}{Context handle returned by [llama_new_context]}

\item{prompt}{Character string prompt}

\item{max_new_tokens}{Maximum number of tokens to generate}

\item{temp}{Sampling temperature. 0 = greedy decoding.}

\item{top_k}{Top-K filtering (0 = disabled)}

\item{top_p}{Top-P (nucleus) filtering (1.0 = disabled)}

\item{seed}{Random seed for sampling}

\item{min_p}{Min-P filtering threshold (0.0 = disabled)}

\item{typical_p}{Locally typical sampling threshold (1.0 = disabled)}

\item{repeat_penalty}{Repetition penalty (1.0 = disabled)}

\item{repeat_last_n}{Number of last tokens to penalize (0 = disabled, -1 = context size)}

\item{frequency_penalty}{Frequency penalty (0.0 = disabled)}

\item{presence_penalty}{Presence penalty (0.0 = disabled)}

\item{mirostat}{Mirostat sampling mode (0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)}

\item{mirostat_tau}{Mirostat target entropy (tau parameter)}

\item{mirostat_eta}{Mirostat learning rate (eta parameter)}

\item{grammar}{GBNF grammar string for constrained generation (NULL = disabled)}

\item{trigger_patterns}{Character vector of regular expressions that lazily
activate a grammar (e.g. the prefix a model emits before a tool call). Only
meaningful alongside a lazy \code{grammar}; \code{NULL} (default) applies the
grammar from the first token. Supplied by \code{\link{llama_chat_build}}.}

\item{trigger_tokens}{Integer vector of token IDs that lazily activate the
grammar, the token-level counterpart to \code{trigger_patterns}. \code{NULL}
(default) means none.}
}
\value{
An external pointer holding the generation state. Pass it to
  [llama_gen_next] and [llama_gen_end]. The underlying sampler is freed
  automatically by the garbage collector.
}
\description{
Sets up sampling and prefills the prompt, returning an opaque state handle
that is pulled one chunk at a time with [llama_gen_next]. This is the
streaming counterpart to [llama_generate]: same sampler chain and the same
output for a given seed, but text arrives incrementally so it can be pushed
into an SSE stream as it is produced.
}
\details{
Typical loop:
\preformatted{
st <- llama_gen_begin(ctx, prompt)
repeat {
  chunk <- llama_gen_next(st)
  if (is.null(chunk)) break
  cat(chunk)
}
cat(llama_gen_end(st))  # flush any held-back trailing bytes
}

Only one streaming generation may be active per context at a time: each
call to \code{llama_gen_begin} clears the context KV cache.
}
\seealso{
[llama_gen_next], [llama_gen_end], [llama_generate]
}
