GitHub

language_model

API reference for language_model.

Anthropic's Messages API as a LanguageModel (Effect's AnthropicLanguageModel)::

program.pipe(fx.provide(language_model.model("claude-sonnet-5-5").layer))
    .pipe(fx.provide(client.layer(api_key=key)))

Config holds request fields (max_tokens, temperature, thinking, ...) sent as they are, and four options of the library: disable_parallel_tool_calls, structured_outputs (use native JSON outputs and strict tools; the default follows the model), mid_conversation_system_messages (send later system messages where they are, when the model takes them) and strict_json_schema (the default strictness of tools). with_config_override changes it for an effect.

Prompt parts read Anthropic options under options["anthropic"] (cacheControl, documentTitle, documentContext, citations, a reasoning part's info) and response parts carry Anthropic metadata under metadata["anthropic"], both in Effect's spelling.

Types

TypeDefinitionDescription
Modeltype Model = Literal['claude-haiku-5-5', 'claude-sonnet-5-5', 'claude-fable-5-1', 'claude-opus-5-5', 'claude-mythos-5-1', 'claude-sonnet-5', 'claude-fable-5', 'claude-mythos-5', 'claude-opus-5', 'claude-opus-4-8', 'claude-opus-4-7', 'claude-mythos-preview', 'claude-opus-4-6', 'claude-sonnet-4-6', 'claude-haiku-4-5', 'claude-haiku-4-5-20251001', 'claude-opus-4-5', 'claude-opus-4-5-20251101', 'claude-sonnet-4-5', 'claude-sonnet-4-5-20250929']

Model names (any other string is accepted too).

Classes

Functions

model_capabilities

model_capabilities(model_id) -> Capabilities

What a model supports, by its name (unknown names get the newest models' capabilities).

ParameterTypeDescription
model_idstr

Returns Capabilities

def model_capabilities(model_id: str) -> Capabilities:
    """What a model supports, by its name (unknown names get the newest
    models' capabilities)."""
    for names, max_tokens, structured in _LEGACY:
        if any(name in model_id for name in names):
            return Capabilities(max_tokens, structured, False)
    mid_conversation = not (
        any(name in model_id for name in _NO_MID_CONVERSATION) or _SONNET_5.search(model_id)
    )
    return Capabilities(128000, True, mid_conversation)

config_capabilities

config_capabilities(config) -> Capabilities
ParameterTypeDescription
configConfigService

Returns Capabilities

def config_capabilities(config: ConfigService) -> Capabilities:
    capabilities = model_capabilities(config.get("model", ""))
    structured = config.get("structured_outputs")
    mid = config.get("mid_conversation_system_messages")
    return replace(
        capabilities,
        supports_structured_output=capabilities.supports_structured_output
        if structured is None
        else structured,
        supports_mid_conversation_system_messages=capabilities.supports_mid_conversation_system_messages
        if mid is None
        else mid,
    )

make

make(*, model, config=None) -> Effect[lm.Service, Never, AnthropicClient]

A LanguageModel for model over the AnthropicClient; a provided Config overrides config per request.

ParameterTypeDescription
modelModel | str
configConfigService | None = None

Returns Effect[lm.Service, Never, AnthropicClient]

def make(
    *, model: Model | str, config: ConfigService | None = None
) -> Effect[lm.Service, Never, AnthropicClient]:
    """A ``LanguageModel`` for ``model`` over the ``AnthropicClient``; a
    provided ``Config`` overrides ``config`` per request."""

    def current() -> Effect[ConfigService]:
        return P.current_config(Config, {"model": model}, config)

    def build(client: ClientService) -> Effect[lm.Service]:
        def generate(options: lm.ProviderOptions) -> Effect[Sequence[lm.EncodedPart], AiError, Any]:
            names = NameMapper(options.tools)

            def send(request: _Request) -> Effect[list[Part], AiError, Any]:
                _annotate_request(options.span, request.payload)

                def respond(sent: tuple[W.Message, Any]) -> Effect[list[Part], AiError, Any]:
                    message, response = sent
                    _annotate_response(options.span, message)
                    return make_response(options, request.codecs, message, response, names)

                return client.create_message(request.payload, request.params).flat_map(respond)

            return current().flat_map(lambda cfg: _make_request(cfg, options, names)).flat_map(send)

        def stream(options: lm.ProviderOptions) -> Stream[lm.EncodedPart, AiError, Any]:
            names = NameMapper(options.tools)

            def send(request: _Request) -> Effect[Stream[Part, AiError, Any], AiError]:
                _annotate_request(options.span, request.payload)
                return client.create_message_stream(request.payload, request.params).map(
                    lambda sent: make_stream_response(
                        options,
                        sent[1],
                        sent[0],
                        names,
                        codecs=request.codecs,
                        max_chars=client.max_response_bytes,
                    )
                )

            built: Effect[Stream[Part, AiError, Any], AiError] = (
                current().flat_map(lambda cfg: _make_request(cfg, options, names)).flat_map(send)
            )
            return St.unwrap(built).map(lambda part: _annotate_stream_part(options.span, part))

        return lm.make(
            generate_text=generate,
            stream_text=stream,
            codec_transformer=to_codec_anthropic,
            supports_system_messages_in_history=current().map(
                lambda cfg: config_capabilities(cfg).supports_mid_conversation_system_messages
            ),
        )

    return AnthropicClient().flat_map(build)

layer

layer(*, model, config=None) -> L.Layer[lm.LanguageModel, Never, AnthropicClient]
ParameterTypeDescription
modelModel | str
configConfigService | None = None

Returns L.Layer[lm.LanguageModel, Never, AnthropicClient]

def layer(
    *, model: Model | str, config: ConfigService | None = None
) -> L.Layer[lm.LanguageModel, Never, AnthropicClient]:
    return L.effect(lm.LanguageModel, make(model=model, config=config))

model

model(model, config=None) -> M.Model[lm.LanguageModel, AnthropicClient]

The model as a Model (provider "anthropic").

ParameterTypeDescription
modelModel | str
configConfigService | None = None

Returns M.Model[lm.LanguageModel, AnthropicClient]

def model(
    model: Model | str, config: ConfigService | None = None
) -> M.Model[lm.LanguageModel, AnthropicClient]:
    """The model as a ``Model`` (provider ``"anthropic"``)."""
    return M.make("anthropic", model, layer(model=model, config=config))

with_config_override

with_config_override(overrides) -> P.WithConfigOverride[ConfigService]

Run an effect with overrides merged over the current Config. Use with .pipe.

ParameterTypeDescription
overridesConfigService

Returns P.WithConfigOverride[ConfigService]

def with_config_override(overrides: ConfigService) -> P.WithConfigOverride[ConfigService]:
    """Run an effect with ``overrides`` merged over the current ``Config``.
    Use with ``.pipe``."""
    return P.WithConfigOverride(Config, overrides)