Reference Messages
messages.create
Sends a request and waits for the whole reply.
Reference#
create(request, options?)#
def create(request: MessageRequest, options: RequestOptions): F[Message]
def create(request: MessageRequest): F[Message]
Parameters
request: aMessageRequest.options: optionalRequestOptionsfor this call.
Returns
The model's Message. Failures are raised in F as AnthropicError.
Caveats
- A request with a large
maxTokenscould outlast the timeout. Before sending, the client estimates its time (an hour for 128,000 tokens) and fails fast withStreamingRequiredwhen that exceeds the timeout. Usestream(request).finalMessagefor those, or give the call its own timeout withRequestOptions.withTimeout: the guard applies only to the client timeout. - Always check
stopReasonbefore trusting the content: a refusal or aMaxTokensstop means it is absent or cut short.
createRaw(request, options)#
def createRaw(request: MessageRequest, options: RequestOptions): F[ApiResponse[Message]]
The same call, returning the reply inside an ApiResponse with the status, the headers (rate limits, for example) and the request id.
Usage#
A single reply#
import anthropic4s._
import cats.effect.{IO, IOApp}
object Hello extends IOApp.Simple {
def run: IO[Unit] =
Anthropic4s.fromEnv[IO].use { client => // reads ANTHROPIC_API_KEY
val request = MessageRequest(Model.ClaudeOpus5_5, maxTokens = 16000, MessageParam.user("Why do cats purr?"))
client.messages.create(request).flatMap(reply => IO.println(reply.text))
}
}
More in the Quick start.
Every signature and example on this page is copied, character for character, from the public source of anthropic4s and its Scaladoc. The complete API, every type included, is in the Scaladoc of each module.