Base Classes¶
Bases: ABC
Abstract base class for LLM provider implementations.
Provides a unified interface for interacting with different LLM providers (OpenAI, Anthropic, Gemini) with automatic retry logic and cost tracking.
Subclasses must implement the :meth:get_response method. Other methods
have default implementations that can be overridden for provider-specific
optimizations.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
The LLM provider name (e.g., "openai", "anthropic", "gemini"). |
|
model |
The specific model identifier (e.g., "gpt-4o", "claude-sonnet-4-20250514"). |
|
input_cost |
Cost per million input tokens in USD. |
|
output_cost |
Cost per million output tokens in USD. |
|
supports_temperature_top_p |
Whether the model supports temperature/top_p params. |
|
use_web_search |
Whether to enable web search (Anthropic only). |
|
api_key_hash |
Truncated SHA256 hash of the API key (for logging). |
|
api_key_alias |
Optional human-readable name for the API key. |
Example
from majordomo_llm import get_llm_instance llm = get_llm_instance("anthropic", "claude-sonnet-4-20250514") response = await llm.get_response("What is 2+2?") print(response.content) 4 print(f"Cost: ${response.total_cost:.6f}")
Source code in majordomo_llm/base.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | |
__init__ ¶
__init__(provider, model, input_cost, output_cost, supports_temperature_top_p=True, use_web_search=False, api_key=None, api_key_alias=None, base_url=None, default_headers=None, hook_pipeline=None)
Initialize the LLM instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
str
|
The LLM provider name. |
required |
model
|
str
|
The model identifier. |
required |
input_cost
|
float
|
Cost per million input tokens in USD. |
required |
output_cost
|
float
|
Cost per million output tokens in USD. |
required |
supports_temperature_top_p
|
bool
|
Whether temperature/top_p are supported. |
True
|
use_web_search
|
bool
|
Enable web search capability (Anthropic only). |
False
|
api_key
|
str | None
|
The API key (used to compute hash for logging). |
None
|
api_key_alias
|
str | None
|
Optional human-readable name for the API key. |
None
|
base_url
|
str | None
|
Optional custom base URL for routing through a proxy. |
None
|
default_headers
|
dict[str, str] | None
|
Optional headers sent with every request. |
None
|
hook_pipeline
|
HookPipeline | None
|
Optional :class: |
None
|
Source code in majordomo_llm/base.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
get_full_model_name ¶
get_full_model_name()
Get the fully qualified model name.
Returns:
| Type | Description |
|---|---|
str
|
Model name in the format "provider:model" (e.g., "anthropic:claude-sonnet-4-20250514"). |
Source code in majordomo_llm/base.py
573 574 575 576 577 578 579 | |
get_json_response
async
¶
get_json_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a JSON response from the LLM.
Automatically parses the LLM's text response as JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str
|
The user's input prompt. |
required |
system_prompt
|
str | None
|
Optional system prompt to set context/behavior. |
None
|
temperature
|
float
|
Sampling temperature (0.0-2.0). Lower is more deterministic. |
0.3
|
top_p
|
float
|
Nucleus sampling parameter (0.0-1.0). |
1.0
|
extra_headers
|
dict[str, str] | None
|
Optional per-request headers merged with default_headers. |
None
|
Returns:
| Type | Description |
|---|---|
LLMJSONResponse
|
LLMJSONResponse containing the parsed JSON dict and usage metrics. |
Raises:
| Type | Description |
|---|---|
HookBlocked
|
If a hook in the pipeline blocks the call. |
ResponseParsingError
|
If the response cannot be parsed as JSON. |
Exception
|
If the API request fails after retries. |
Source code in majordomo_llm/base.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
get_json_schema_response
async
¶
get_json_schema_response(user_prompt, response_schema, system_prompt=None, schema_name='Response', schema_description=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None, **kwargs)
Get a structured JSON response validated against a raw JSON schema.
Runs the optional :attr:hook_pipeline around the provider call.
Hooks see the raw provider JSON text in after_call before
downstream pydantic/JSON-schema parsing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str
|
The user's input prompt. |
required |
response_schema
|
dict[str, Any]
|
Raw JSON schema dict defining the expected response. |
required |
system_prompt
|
str | None
|
Optional system prompt to set context/behavior. |
None
|
schema_name
|
str
|
Provider-facing schema/tool name. |
'Response'
|
schema_description
|
str | None
|
Optional provider-facing schema/tool description. |
None
|
temperature
|
float
|
Sampling temperature (0.0-2.0). |
0.3
|
top_p
|
float
|
Nucleus sampling parameter (0.0-1.0). |
1.0
|
extra_headers
|
dict[str, str] | None
|
Optional per-request headers merged with default_headers. |
None
|
caller_metadata
|
dict[str, Any] | None
|
Free-form dict forwarded to every hook. |
None
|
**kwargs
|
Any
|
Reserved for future provider-specific passthrough arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
LLMResponse
|
LLMResponse whose content is canonical JSON with sorted keys and no extra whitespace. |
Raises:
| Type | Description |
|---|---|
HookBlocked
|
If a hook in the pipeline blocks the call. |
Source code in majordomo_llm/base.py
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |
get_response
async
¶
get_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a plain text response from the LLM.
Runs the optional :attr:hook_pipeline around the provider call.
Hooks see the prompt before the call and the response text after.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_prompt
|
str
|
The user's input prompt. |
required |
system_prompt
|
str | None
|
Optional system prompt to set context/behavior. |
None
|
temperature
|
float
|
Sampling temperature (0.0-2.0). Lower is more deterministic. |
0.3
|
top_p
|
float
|
Nucleus sampling parameter (0.0-1.0). |
1.0
|
extra_headers
|
dict[str, str] | None
|
Optional per-request headers merged with default_headers. |
None
|
caller_metadata
|
dict[str, Any] | None
|
Free-form dict forwarded to every hook via
:class: |
None
|
Returns:
| Type | Description |
|---|---|
LLMResponse
|
LLMResponse containing the text content and usage metrics. |
Raises:
| Type | Description |
|---|---|
HookBlocked
|
If a hook in the pipeline blocks the call. |
Exception
|
If the API request fails after retries. |
Source code in majordomo_llm/base.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
get_response_stream
async
¶
get_response_stream(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a streaming text response from the LLM.
Hooks do not run on streaming responses; caller_metadata is
accepted for API symmetry and ignored.
Source code in majordomo_llm/base.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
get_structured_json_response
async
¶
get_structured_json_response(response_model, user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a structured response validated against a Pydantic model.
Uses provider-specific mechanisms (tool calling, response schemas) to ensure the response conforms to the specified Pydantic model schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_model
|
type[T]
|
Pydantic model class defining the expected structure. |
required |
user_prompt
|
str
|
The user's input prompt. |
required |
system_prompt
|
str | None
|
Optional system prompt to set context/behavior. |
None
|
temperature
|
float
|
Sampling temperature (0.0-2.0). Lower is more deterministic. |
0.3
|
top_p
|
float
|
Nucleus sampling parameter (0.0-1.0). |
1.0
|
Returns:
| Type | Description |
|---|---|
LLMStructuredResponse
|
LLMStructuredResponse containing the validated Pydantic model instance. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the response doesn't match the model schema. |
Exception
|
If the API request fails after retries. |
Example
from pydantic import BaseModel class Person(BaseModel): ... name: str ... age: int response = await llm.get_structured_json_response( ... response_model=Person, ... user_prompt="Extract: John is 30 years old", ... ) print(response.content.name) John
Source code in majordomo_llm/base.py
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | |
Bases: Usage
Response from an LLM containing plain text content.
Inherits all usage metrics from :class:Usage.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str
|
The text content of the LLM response. |
deprecation_warning |
str | None
|
Warning if a deprecated model was auto-replaced. |
Source code in majordomo_llm/base.py
355 356 357 358 359 360 361 362 363 364 365 366 367 | |
Async-iterable wrapper around a streaming LLM response.
Yields text chunks as they arrive. After iteration completes, usage
and cost data is available via the :attr:usage property.
Example
stream = await llm.get_response_stream("Hello") async for chunk in stream: ... print(chunk, end="") print(stream.usage.total_cost)
Source code in majordomo_llm/base.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
collect
async
¶
collect()
Consume the entire stream and return an :class:LLMResponse.
Source code in majordomo_llm/base.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
Bases: Usage
Response from an LLM containing parsed JSON content.
Inherits all usage metrics from :class:Usage.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
dict[str, Any]
|
The parsed JSON content as a Python dict. |
Source code in majordomo_llm/base.py
370 371 372 373 374 375 376 377 378 379 380 | |
Bases: Usage
Response from an LLM containing a validated Pydantic model.
Inherits all usage metrics from :class:Usage.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
BaseModel
|
The validated Pydantic model instance. |
Source code in majordomo_llm/base.py
383 384 385 386 387 388 389 390 391 392 393 | |